473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Remove duplicate object values in array

Hello - I've read a ton of stuff about deleting duplicate values in an array. They are all very useful - they just haven't addressed an array of objects.

Here's my array:
[HTML]
var sDat = [{
sid:12,
scode:"code",
sname:"Sam"
},
{
sid:12,
scode:"code",
sname:"Sam"
},
{
sid:139,
scode:"code",
sname:"Jake"
}];
[/HTML]
The array is produced from JSON sent to me from a database query. I can't do a "group by" in the sql query - i actually need the repeats in a different array.

Anyway - I want to be able to remove duplicate sDat.sid entries in my sDat array - but I'm having difficulties discovering a way to handle it.

Any ideas?
Nov 13 '06 #1
1 16569
gits
5,390 Expert Mod 4TB
hi ...

for such tasks we may use something like the following example:

Expand|Select|Wrap|Line Numbers
  1. var sDat = [
  2.     { sid:12, scode:"code", sname:"Sam" },
  3.     { sid:12, scode:"code", sname:"Sam" },
  4.     { sid:139, scode:"code", sname:"Jake"}
  5. ];
  6.  
  7. function cleanup(arr, prop) {
  8.     var new_arr = [];
  9.     var lookup  = {};
  10.  
  11.     for (var i in arr) {
  12.         lookup[arr[i][prop]] = arr[i];
  13.     }
  14.  
  15.     for (i in lookup) {
  16.         new_arr.push(lookup[i]);
  17.     }
  18.  
  19.     return new_arr;
  20. }
  21.  
  22. var n = cleanup(sDat, 'sid');
  23.  
that makes use of javascript objects and the for-in loop construct ...

kind regards
Apr 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: NiQ | last post by:
Hello every one, this is about an array of string and may be have done several time before but i need it and wasnt able to find it so here is the problem i have an array of strings with contains...
2
by: Patrick G. | last post by:
Greetings all: ASP VB, SQL Svr 2000 I am pulling data from 3 tables. table1 holds item details table2 holds publication types and the item id from table1 table3 holds category types and...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
5
by: dale.zjc | last post by:
I've got the following table data: 11652 5.99 11652 0.14 12996 5.03 12996 0.12 12996 7.00 And I need to write a query to return only rows 2 and 4, since the remaining rows have duplicate...
19
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
4
by: whatnameisnottaken | last post by:
right now i have gotten all the classes to print out using helpdbg. Now i want to remove the duplicate class names out of an array so i can just print one of each. This is the output i have. ...
10
chandru8
by: chandru8 | last post by:
hi to all iam using vb6.0 can any one tell me how to duplicate values in a array if possible i want to delete the duplicate values its urgent thanks
2
by: dawn123 | last post by:
I have a 2-d array that the user enters tress species and diameter so it looks something like: sm 10 rm 18 bf 12 sm10 sm10 rm18 and so on.. I need to be able to seach though my array and find...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.