简体   繁体   English

JS检查对象的值是否存在

[英]JS check if the value of object exists

So, I have following js setup: 因此,我有以下js设置:

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. 在此示例中,我将数字硬编码为“ 185”。 I need to check if the id of 185 exists, then skip to else . 我需要检查id of 185存在,然后跳至else I am not sure how to check it. 我不确定如何检查。 I tried typeof , undefined etc. but no luck. 我尝试了typeofundefined等,但是没有运气。 (In other words, I should only have one "185"). (换句话说,我应该只有一个“ 185”)。

Any help? 有什么帮助吗? Thanks! 谢谢!

If I understood correctly what you are trying to achieve, you have to iterate over NAMES and check every element. 如果我正确理解您要实现的目标,则必须遍历NAMES并检查每个元素。 For example, you could do it using [].some javascript function: 例如,您可以使用[].some javascript函数来实现:

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 如果您要删除重复项,则可以使用NAMES作为对象而不是像这样的数组

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. 首先,我建议使用此工具制作JS Fiddle或CodePen,以便人们可以看到代码正在运行。

I believe that the issue is that NAMES[my_name] is not doing what you think it is. 我认为问题在于NAMES[my_name]没有按照您的想法做。 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. NAMES是一个数组,因此当您说NAMES[my_name]您实际上是在数组中查询ITEM,因此您可以获取在INFO函数中创建的整个对象。 What you really want is to see if the object has an attribute that matches the value (eg "185" from the my_names array). 您真正想要的是查看对象是否具有与值匹配的属性(例如,my_names数组中的“ 185”)。

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"). 请注意,如果它查看NAMES[x][my_name] -这是在询问数组索引“ x”处的项目是否具有“ my_name”属性(例如“ 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. 在午夜之后,我向您保证,还有更多简洁明了的JS可以执行此操作,但这应该向您显示您必须解决的基本问题。

Try this code using hasOwnProperty method : 使用hasOwnProperty方法尝试以下代码:

 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{ } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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