Only add if not already in place

Posted by Woppie on Stack Overflow See other posts from Stack Overflow or by Woppie
Published on 2011-01-02T06:20:23Z Indexed on 2011/01/02 6:53 UTC
Read the original article Hit count: 111

Filed under:
|

Here's my data structure:

var data = [
 { id: '1924', info: 'boo' },
 { id: '1967', info: 'foo' }
];

The id value should be unique, but the info may not be unique. How would I add new data into the data hash only if the id of the new data is unique?

Is the only way to iterate over the whole hash and see if there is such an id already in place?

data.push({ id: '1967', info: 'goo-goo' }); //should not be added
data.push({ id: '1963', info: 'goo-goo' }); //should be added

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about data-structures