1

I have a JS Object like so:

var ob{
  1 : {
        id:101,
        name:'john',
        email:'[email protected]'
      },
  2 : {
        id:102,
        name:'jim',
        email:'[email protected]'
      },
  3 : {
        id:103,
        name:'bob',
        email:'[email protected]'
      },
}

I want to check if this JS object already contains a record. If it doesn't then I want to add it. For example.

if(ob contains an id of 101){
//don't add john
}else{
//add john
}

Catch my drift? Pretty simple question. I just want to know the best way of doing it.

Thanks Guys!

W.

1
  • Should "ob" really be an object with sequential numeric properties, or do you really want an array of objects? var obj = [ {...}, {...}, {...} ]; Commented Jan 4, 2011 at 21:53

5 Answers 5

3

Simple:

function contains_id(obj, id) {
   for(var key in obj)
     if(obj.hasOwnProperty(key) && 
        obj[key]['id'] && obj[key]['id'] == id) 
        return true;
   return false;
}

var ob = ...
if(contains_id(ob, 2)) {
   //do something...
} else {
   //do something else...
}
Sign up to request clarification or add additional context in comments.

Comments

3
var found = false;
for(var nr in ob) {
  if(ob.hasOwnProperty(nr)) {
    if(ob[nr].id === 103) {
       found=true;
       break;
    } 
  } 
}
if(!found) {
  //add ...
}

It would've been easier if you could use the id as key for the object

2 Comments

@JacobRelkin But you have so much rep, you don't need any more. :) OK, you can have a cookie, too.
@Phrogz LOL. You can never have enough rep. xDDD
1

Is it problem to use id as key of your object? Like that:

var ob{
  101 : {
        id:101,
        name:'john',
        email:'[email protected]'
      },
  102 : {
        id:102,
        name:'jim',
        email:'[email protected]'
      },
  103 : {
        id:103,
        name:'bob',
        email:'[email protected]'
      },
}

if(ob[101]){
//don't add john
}else{
//add john
}

Comments

0

I am quite certain you cant do that without a loop as far as IN is concerned is to check the object's em how to say it...properties for example if the "id" exists in your example and not the variable

Comments

0

Unless they are indexed by the value you are checking for, I believe you need to iterate over all elements in the object and check each one.

function recordExistsById(id, ob) {
    for (var key in ob) {
        if (ob[key].hasOwnProperty("id") && ob[key].id == id) {
            return true;
        }
    }
    return false;
}

2 Comments

@rr There are two problems with this: 1) You did not var your record variable, so you are corrupting global variables 2) You aren't using hasOwnProperty, so record may be set to any properties added to Object.prototype.
@rr you're missing the point, you need to use hasOwnProperty for the object that you use in the for .. in because it will enumerate all properties in the prototype chain. It is not necessary to use it on the ob[key] because you already know that all of those objects have an id property.

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.