我有两个对象数组。
Array1 : [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
Array2 : [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
这里
array1 的类名称为 A、B、C 和 D。
array2 的类名称为 X、Y、A、C 和 Z
函数应该以不属于classNames
的array2
方式返回classNames
array1
该函数的返回将是一个包含 X、Y 和 Z 作为元素的数组。
这个函数怎么写javascript
,时间复杂度更小,因为array1和array2可能有20多个对象。
编辑
这是我使用的脚本for loop
array1 = [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
array2 = [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
function findSuggest(){
var sug = [];
for(array2_count=0;array2_count < array2.length;array2_count++){
for(array1_count=0;array1_count < array1.length;array1_count++){
if(array2[array2_count].className == array1[array1_count].className){
break;
}
else{
if(array1_count == (array1.length - 1)){
sug[sug.length] = array2[array2_count].className;
}
}
}
}
}
这里sug[]
会有所有的建议。