比较数组和 json 数据
compare array and json data
http://jsfiddle.net/0czdrn7t/4/
我尝试比较两个数组,
在数组 arrayB
中查找对象 (Type
= 4) 并且如果所有对象 (Type
= 4) Privilege
与 arrayA
值不匹配, 然后插入到 arrayC
.
对于这个例子,arrayA[0]
是 1 已经在 arrayB
最后一个对象中匹配然后我不希望它推入结果,我的代码有问题吗??
var arrayA = [1];
var arrayB = [ { Type: 1,
Privilege: 0,
},
{ Type: 3,
Privilege: 0,
},
{ Type: 3,
Privilege: 1,
},
{ Type: 4,
Privilege: 1,
} ];
var arrayC = [];
var type = 4;
for (var i = 0; i < arrayA.length; i++) {
var insertValidate = true;
var issetTypeValidate = false;
for (var ii = 0; ii < arrayB.length; ii++) {
if (arrayB[ii].Type == type) {
if (arrayB[ii].Privilege != arrayA[i]) {
insertValidate = false;
// break;
}
issetTypeValidate = true;
}
}
if (issetTypeValidate == true) {
if (insertValidate == true) {
var o = {};
o.Type = type;
o.Privilege = arrayA[i];
arrayC.push(o);
}
} else {
// var o = {};
// o.Type = type;
// o.Privilege = arrayA[i];
// arrayC.push(o);
}
}
console.log(arrayC);
试试这个:
var arrayA = [1];
var arrayB = [ { Type: 1,
Privilege: 0,
},
{ Type: 3,
Privilege: 0,
},
{ Type: 3,
Privilege: 1,
},
{ Type: 4,
Privilege: 1,
} ];
var arrayC = [];
var type = 4;
for (var i = 0; i < arrayA.length; i++) {
var insertValidate = true;
var issetTypeValidate = false;
for (var ii = 0; ii < arrayB.length; ii++) {
if (arrayB[ii].Type == type) {
if (arrayB[ii].Privilege == arrayA[i]) {
// alert(arrayB[ii].Privilege +'='+arrayA[i])
// if at least one of them is equal, than don't print it
insertValidate *= false;
}
issetTypeValidate = true;
}
}
if (issetTypeValidate == true) {
if (insertValidate == true) {
var o = {};
o.Type = type;
o.Privilege = arrayA[i];
arrayC.push(o);
}
} else {
}
}
alert(JSON.stringify(arrayC));
因为updateRoleList[ii].Privilege != inputUserRoleType4[i]
returns false
当 updateRoleList[ii].Privilege
等于 1
并且 inputUserRoleType4[i]
也是 1
.
http://jsfiddle.net/0czdrn7t/4/
我尝试比较两个数组,
在数组 arrayB
中查找对象 (Type
= 4) 并且如果所有对象 (Type
= 4) Privilege
与 arrayA
值不匹配, 然后插入到 arrayC
.
对于这个例子,arrayA[0]
是 1 已经在 arrayB
最后一个对象中匹配然后我不希望它推入结果,我的代码有问题吗??
var arrayA = [1];
var arrayB = [ { Type: 1,
Privilege: 0,
},
{ Type: 3,
Privilege: 0,
},
{ Type: 3,
Privilege: 1,
},
{ Type: 4,
Privilege: 1,
} ];
var arrayC = [];
var type = 4;
for (var i = 0; i < arrayA.length; i++) {
var insertValidate = true;
var issetTypeValidate = false;
for (var ii = 0; ii < arrayB.length; ii++) {
if (arrayB[ii].Type == type) {
if (arrayB[ii].Privilege != arrayA[i]) {
insertValidate = false;
// break;
}
issetTypeValidate = true;
}
}
if (issetTypeValidate == true) {
if (insertValidate == true) {
var o = {};
o.Type = type;
o.Privilege = arrayA[i];
arrayC.push(o);
}
} else {
// var o = {};
// o.Type = type;
// o.Privilege = arrayA[i];
// arrayC.push(o);
}
}
console.log(arrayC);
试试这个:
var arrayA = [1];
var arrayB = [ { Type: 1,
Privilege: 0,
},
{ Type: 3,
Privilege: 0,
},
{ Type: 3,
Privilege: 1,
},
{ Type: 4,
Privilege: 1,
} ];
var arrayC = [];
var type = 4;
for (var i = 0; i < arrayA.length; i++) {
var insertValidate = true;
var issetTypeValidate = false;
for (var ii = 0; ii < arrayB.length; ii++) {
if (arrayB[ii].Type == type) {
if (arrayB[ii].Privilege == arrayA[i]) {
// alert(arrayB[ii].Privilege +'='+arrayA[i])
// if at least one of them is equal, than don't print it
insertValidate *= false;
}
issetTypeValidate = true;
}
}
if (issetTypeValidate == true) {
if (insertValidate == true) {
var o = {};
o.Type = type;
o.Privilege = arrayA[i];
arrayC.push(o);
}
} else {
}
}
alert(JSON.stringify(arrayC));
因为updateRoleList[ii].Privilege != inputUserRoleType4[i]
returns false
当 updateRoleList[ii].Privilege
等于 1
并且 inputUserRoleType4[i]
也是 1
.