简体   繁体   English

检查值是否存在于多维数组中并返回布尔值

[英]check if value exists in mulitdimensional array and return boolean

I have seen many posts about searching nested arrays in javascript but none of them have a way to tell if an item does not exist in the array. 我见过很多有关在javascript中搜索嵌套数组的文章,但是它们都没有办法告诉数组中是否不存在任何项。

this may sound confusing so here are some examples: 这听起来可能令人困惑,所以这里有一些示例:

I have 2 arrays oldArray and newArray 我有2个数组oldArraynewArray

structured like this: 结构如下:

array(
      [0]=>array([name]:"name"
           [location]:"location")
      [1]=>array([name]:"name2"
           [location]:"location2")
 )

both arrays are structured this way. 两个阵列都是以这种方式构造的。

I need to be able to know which names exist in the old array and not in the new and vice versa. 我需要能够知道旧数组中存在哪些名称,而不是新数组中存在,反之亦然。

here is what I have tried: 这是我尝试过的:

 var name= oldArray[key]['name'];

   for (var key in oldArray) {
       for(var i= 0, len = newArray.length; i < len; i++){
                if(newArray[i]['name'] == name){
                          //push to array
                }
                 else{
                      //push to different array
                 }
       }
 }

this way I will have an array that contains all names that exist in both arrays and an array that only contains names that exist in the oldArray.. 这样,我将拥有一个数组,其中包含两个数组中都存在的所有名称,以及一个数组,仅包含oldArray中存在的名称。

this doesn't seem to work because it is a 1 to 1 comparison. 这似乎不起作用,因为它是一对一的比较。 the first array is correct but the array that should only contain the names that exist in the old array is not correct. 第一个数组是正确的,但仅应包含旧数组中存在的名称的数组是不正确的。

Have you initialized the array for old values? 您是否为旧值初始化了数组?

Are you getting an exception? 你有例外吗?

Have you debugged it with Chrome? 您是否使用Chrome调试过?

edit: 编辑:

I think if you do: 我认为如果您这样做:

for all old values push if exists and ask again and push, it will keep pushing in an infinite loop. 对于所有旧值,push如果存在,然后再次询问并推送,它将继续以无限循环进行推送。

try to add a break 尝试加个休息

    var name= oldArray[key]['name'];

       for (var key in oldArray) {
           for(var i= 0, len = newArray.length; i < len; i++){
                    if(newArray[i]['name'] == name){
                              //push to array

                    break;
                    }
                     else{
                         if(i==newArray.length-1)
                          //push to different array


                     }
           }
     }

Use a boolean to keep track of whether there is a match in the new array. 使用布尔值跟踪新数组中是否存在匹配项。 Then after you complete the inner for loop over the newArray, check boolean to see if no matches were found, then push to other array. 然后,在完成newArray的内部for循环之后,检查boolean以查看是否未找到匹配项,然后推送到其他数组。

var name= oldArray[key]['name'];

for (var key in oldArray) 
{
    var foundMatch=false
    for(var i= 0, len = newArray.length; i < len; i++)
    {
        if(newArray[i]['name'] == name)
        {
            //push to array
            foundMatch=true;
                    break;
        }
    }
    if(foundMatch==false)
    //push to different array

}

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

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