相似度我有2列:我怎樣才能找到2個陣列
var arr1 = ['2','3','4'];
var arr2 = ['2'] or ['2', '3'] or ['2','3','4'] or ['3', '4', '5'] or ['5','6'];
如果ARR2具有相似ARR1返回true,如果沒有類似像[「5」,「6」]則返回false當arr2是['3','4','5']時甚至返回false。
相似度我有2列:我怎樣才能找到2個陣列
var arr1 = ['2','3','4'];
var arr2 = ['2'] or ['2', '3'] or ['2','3','4'] or ['3', '4', '5'] or ['5','6'];
如果ARR2具有相似ARR1返回true,如果沒有類似像[「5」,「6」]則返回false當arr2是['3','4','5']時甚至返回false。
var arr1 = ['2', '3', '5', '7'];
var arr2 = ['3', '4', '8'];
for(i=0; i<arr2.length; i++){
if(jQuery.inArray(arr2[i], arr1)== -1){
console.log(arr2[i] + ' Is not in Arr1');
}
}
4是不是在ARR1 8不ARR1
只使用JSON.stringify(arr)
var arr1 = ['2','3','4'];
var arr2 = ['2','3','4'] ;
var i= JSON.stringify(arr1) === JSON.stringify(arr2);
alert(i);
你能做到這樣也
var array1 = ['2', '3', '5', '7'];
var array2 = ['3','9', '8'];
var diff = [];
$.grep(array2, function(value, key) {
if($.inArray(value, array1) === -1){
diff.push(value);
}
});
if(diff.length > 0){
console.log('diff Array is');
console.log(diff);
console.log('Array2 is not a subset of Array1');
}else{
console.log('Array2 is a subset of Array1');
}
你的意思是說,如果兩個數組中的值相同,它應該返回true? – Spokey 2014-09-03 08:21:18
它不清楚你想要測試什麼? – Alex 2014-09-03 08:21:51
所以你想檢查arr2是否是arr1的子集? – Sami 2014-09-03 08:29:05