简体   繁体   中英

JS check if the value of object exists

So, I have following js setup:

var NAMES = []; 
function INFO(id,first,middle,last){ 
  var newMap = {};
  newMap[id] = [first, middle, last];
  return newMap ;   
}

Then,

for (var j = 0; j < NUMBER.length; j++) {   //let say it there are three values
    var my_name = all_names[j]; // has "185, 185, 185"              
    if (NAMES[my_name] !== 185){ //Needs to check here              
        NAMES.push(INFO(my_name,"sean","sdfsd","sdfsfd"));                      
    }else{

    }               
}
alert(JSON.stringify(NAMES , null, 4));

Here is a screenshot of the alert:

在此处输入图片说明

I hardcoded the number "185" for this example. I need to check if the id of 185 exists, then skip to else . I am not sure how to check it. I tried typeof , undefined etc. but no luck. (In other words, I should only have one "185").

Any help? Thanks!

If I understood correctly what you are trying to achieve, you have to iterate over NAMES and check every element. For example, you could do it using [].some javascript function:

if (!NAMES.some(function(v){return v[my_name]})) {
    ...
} else {

}

If you want to remove duplication you can just use NAMES as an object instead of array like this

var all_names = [185, 185, 181], 
    NAMES = {};
for (var j = 0; j < all_names.length; j++) {   //let say it there are three values
    var my_name = all_names[j]; // has "185, 185, 185"  
    NAMES[my_name] = ["sean","sdfsd","sdfsfd"];
}

alert(JSON.stringify(NAMES, null, 4));

First of all I would recommend making a JS Fiddle or CodePen out of this so people can see the code running.

I believe that the issue is that NAMES[my_name] is not doing what you think it is. NAMES is an Array so when you say NAMES[my_name] you are really asking for the ITEM in the array so you are getting the entire object that you create in the INFO function. What you really want is to see if the object has an attribute that matches the value (eg "185" from the my_names array).

This is not the prettiest code but it will show you how to do what you really want to do:

var NAMES = []; 
function INFO(id,first,middle,last){ 
  var newMap = {};
  newMap[id] = [first, middle, last];
  return newMap ;   
}

all_names = ["185", "186", "185"]

for (var j = 0; j < all_names.length; j++) {
    var my_name = all_names[j]; 
    if (NAMES.length == 0) {
        NAMES.push(INFO(my_name,"sean","sdfsd","sdfsfd"));                      
    } else {
        var match = false;
        for (var x = 0; x < NAMES.length; x++) {
            console.log(NAMES[x][my_name] + ' : ' + my_name);
            if(NAMES[x][my_name]) {
                match = true;
            } 
        }
        if (!match) {
            NAMES.push(INFO(my_name,"sean","sdfsd","sdfsfd"));
        }
    }
}
alert(JSON.stringify(NAMES , null, 4));

Note the if that looks at NAMES[x][my_name] - this is asking if the item at array index 'x' has an attribute of 'my_name' (eg "185"). I believe this is really what you are trying to do. As its after midnight I assure you that there is more concise and pretty JS to do this but this should show you the basic issue you have to address.

Try this code using hasOwnProperty method :

 for (var j = 0; j < NUMBER.length; j++) { //let say it there are three values var my_name = all_names[j]; // has "185, 185, 185" if (!NAMES[my_name].hasOwnProperty("185")){ //Needs to check here NAMES.push(INFO(my_name,"sean","sdfsd","sdfsfd")); }else{ } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM