0

Suppose I have:

var original = {

    0: {
        id : 158    
    },
    1: {
        id : 159    
    }

}

var newSeries = {

    0: {
        id : 158,
        name: "Jason",
        lastname: "Smith",
        status : "active"   
    },
    1: {
        id : 159    ,
        name: "Rebecca",
        lastname: "Smith",
        status : "terminated"   
    }

}

and I want to know for example id # 158 from newSeries exists in original, how would I go about doing so? I found a partial solution to use the in operator but that only compares their keys and not the keys and values.

3
  • In your loop, if the key is in, then check the value at newSeries[i]['id'] Commented Jul 10, 2014 at 3:33
  • Given the way your data is currently structured, there's no other way to find the id value other than the brute force way of iterating all the keys and checking for an id match. You could restructure your data to add an index by id to allow a direct id lookup if needed. Commented Jul 10, 2014 at 3:34
  • You can use an array and stackoverflow.com/q/16339635/218196. Either way, what exactly are you having problems with? You must have tried something. Commented Jul 10, 2014 at 4:04

4 Answers 4

1

If you planning to do lots of processing like that, I would commend using underscorejs. It has methods for finding values by passing lamda function, filtering, sorting, merging, converitng and lots of other functionality

With underscorejs its one liner:

_(original).find(function(x) { return x.id == 159; })

Jsbin demo

Sign up to request clarification or add additional context in comments.

2 Comments

This is interesting, though this doesn't return a boolean, but another object that matches the ID we're looking for.
just add !! in front to get boolean
1

Try this:

for (var n in newSeries) {
   console.log('id ' + 
               newSeries[n].id + 
               ' exists: ' +
               (original[n].id === newSeries[n].id));   
}

FIDDLE

1 Comment

Why are you casting original[n].id === newSeries[n].id into a boolean when it's already a boolean expression?
0

You would have to loop through each keys and values to test if id is there.

For instance

var myId = 158;
for (var key in original) {
    if (original[key].id === myId) {
        // here your id has been found
    }
}

If you don't want to loop through your object like this, you will have to change your data structure a bit. You could use ids as a key in your original object and search it like this:

var original = {

    158: {
        id : 158    
    },
    159: {
        id : 159    
    }
};

var myId = 158;

if (myId in original) {
    // here your id has been found
}

1 Comment

I found this to be the simplest solution.
0

If you're targeting ES5:

function isValueInObject(value, object) {

    return Object.keys(object).map(function (key) {
        return object[key];
    }).some(function (_value) { 
        return _value === value; 
    });

}

console.log(isValueInObject('bar', {foo: 'bar'})) // true
console.log(isValueInObject('baz', {foo: 'bar'})) // false

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.