找到下面的代码以获取更多详细信息
var A1 = ["1","2","5","6"];
var A2 = [
{label:"one", value:"1"},
{label:"two", value:"2"},
{label:"three", value:"3"},
{label:"four", value:"4"},
{label:"five", value:"5"},
{label:"six", value:"6"},
];
for(var i=0; i<A2.length; i++){
for(var j=0; j<A1.length; j++ ){
if(A1[i] == A2[j].value){
A2[ i ].disabled == true;
}
else{
A2[ i ].disabled == false;
}
}
}
console.log( JSON.stringify( A2 ) );
我想要以下输出:
[{"label":"one","value":"1","disabled":true},
{"label":"two","value":"2","disabled":true},
{"label":"three","value":"3","disabled":false},
{"label":"four","value":"4","disabled":false},
{"label":"five","value":"5","disabled":true},
{"label":"six","value":"6","disabled":true}]
答案 0 :(得分:1)
您需要一个循环,然后检查该值是否在a2
中。然后将disabled
设置为此布尔值。
在问题中,您将i
和j
混合在一起,并为a1
的任何值设置了一个值。结果或多或少是随机的。
顺便说一句,我将所有变量都更改为小写开头,因为在Javascript中,通常将以大写开头的变量保留给不可理解的函数或类。
var a1 = ["1", "2", "5", "6"],
a2 = [{ label: "one", value: "1" }, { label: "two", value: "2" }, { label: "three", value: "3" }, { label: "four", value: "4" }, { label: "five", value: "5" }, { label: "six", value: "6" }],
i;
for (i = 0; i < a2.length; i++) {
a2[i].disabled = a1.includes(a2[i].value);
}
console.log(a2);
带有注释的代码。
var A1 = ["1", "2", "5", "6"];
var A2 = [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
{ label: "four", value: "4" },
{ label: "five", value: "5" },
{ label: "six", value: "6" },
];
for (var i = 0; i < A2.length; i++) {
A2[i].disabled = false; // do not use a comparison, take an assignment
// set to false as default value
for (var j = 0; j < A1.length; j++) {
if (A1[j] == A2[i].value) { // i and j switched
A2[i].disabled = true; // same here with comparison where you need
// an assignment
}
}
}
console.log(JSON.stringify(A2));
答案 1 :(得分:0)
我认为您在数组中省略了数字“ 3”,即
var A1 = ["1","2","5","6"];
答案 2 :(得分:0)
正在确定范围的原因...
var A1 = ["1", "2", "5", "6"];
var A2 = [{
label: "one",
value: "1"
}, {
label: "two",
value: "2"
}, {
label: "three",
value: "3"
}, {
label: "four",
value: "4"
}, {
label: "five",
value: "5"
}, {
label: "six",
value: "6"
} ];
for (var i = 0; i < A2.length; i++) {
for (var j = 0; j < A1.length; j++) {
if (A1[i] == A2[j].value) {
A2[i].disabled = true;
console.log(JSON.stringify(A2[i]))
} else {
A2[i].disabled = false;
console.log(JSON.stringify(A2[i]))
}
}
}
答案 3 :(得分:0)
map()
方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。
includes()
方法确定数组是否包含某个元素,并在适当时返回true或false。
使用map()
和includes()
似乎很直接:
var A1 = ["1","2","5","6"];
var A2 = [ {label:"one", value:"1"}, {label:"two", value:"2"}, {label:"three", value:"3"}, {label:"four", value:"4"}, {label:"five", value:"5"}, {label:"six", value:"6"}];
A2 = A2.map(i => {
if(A1.includes(i.value))
i.disabled = true;
else
i.disabled = false;
return i;
});
console.log( JSON.stringify(A2) );
答案 4 :(得分:0)
由于处于嵌套循环状态,因此处于最后状态
for(var i=0; i<A2.length; i++){
for(var j=0; j<A1.length; j++ ){
if(A1[i] == A2[j].value){
A2[ i ].disabled == true;
}
else{
A2[ i ].disabled == false;
}
}
}
A2 [i]在每次循环迭代中都会被覆盖,因为它上面的循环会递增j而不是i,因此最后,您只会看到j的最后一个值的影响
已经指出的第二件事是您必须分配
A2[ i ].disabled == true;
使用单个=符号