Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果两个数组包含精确的文本值,则匹配JSON_Javascript_Arrays_Json_Lodash_Underscore.js - Fatal编程技术网

Javascript 如果两个数组包含精确的文本值,则匹配JSON

Javascript 如果两个数组包含精确的文本值,则匹配JSON,javascript,arrays,json,lodash,underscore.js,Javascript,Arrays,Json,Lodash,Underscore.js,目标:查看是否可以匹配两个数组并返回布尔值true或false 问题:我想问是否有更好的解决方案来匹配两个数组的相同值,例如 "arr1": ["hello, world, droids"], "arr2": ["hello, there, Kenobi"] 我从另一个答案中找到了一个答案。但当我们有下划线和lodash这样的实用程序库时,这种方法似乎太牵强了 我的想法是: const __ = require('

目标:查看是否可以匹配两个数组并返回布尔值
true
false

问题:我想问是否有更好的解决方案来匹配两个数组的相同值,例如

"arr1": ["hello, world, droids"],
"arr2": ["hello, there, Kenobi"]
我从另一个答案中找到了一个答案。但当我们有下划线和lodash这样的实用程序库时,这种方法似乎太牵强了

我的想法是:

const __ = require('underscore')

if(__.difference(arr1, arr2).length > 0) {
 console.log('not same')
} else {
 console.log('same')
}
我对编程非常陌生,所以我不确定哪一个应该适合生产,或者我的想法是否正确

我还将在下一步添加我找到的SO答案

var areJSONArraysEqual = function(jsonArray1,
                                    jsonArray2) {

                                if(jsonArray1.length===0||jsonArray2.length===0){
                                    if(jsonArray1.length===0 && jsonArray2.length===0){
                                        return true;
                                    }else{
                                        return false;
                                    }
                                }

                                for(var i=0;i<jsonArray1.length;i++){
                                    for ( var key in jsonArray1[i]) {
                                        if(jsonArray1[i][key].length>1){
                                            return areJSONArraysEqual(jsonArray1[i][key],jsonArray2[i][key])
                                        }
                                        if (jsonArray1[i][key] != jsonArray2[i][key]) {
                                            return false;
                                        }
                                    }
                                }
                                return true;
                            };
var areJSONArraysEqual=函数(jsonArray1,
jsonArray2){
if(jsonArray1.length==0 | | jsonArray2.length==0){
if(jsonArray1.length==0&&jsonArray2.length==0){
返回true;
}否则{
返回false;
}
}
对于(var i=0;i1){
返回areJSONArraysEqual(jsonArray1[i][key],jsonArray2[i][key])
}
if(jsonArray1[i][key]!=jsonArray2[i][key]){
返回false;
}
}
}
返回true;
};

看来您试图做的是一个深度的平等比较。您可以使用下划线(和Lodash)来执行此操作。例如:

const _ = require('underscore');

const arr1 = ["hello, world, droids"];
const arr2 = ["hello, there, Kenobi"];

console.log(_.isEqual(arr1, arr2)); // false