console.log 为:A0004A0003A0001A0005A0004A0002A0001
但我预计 console.log 将为:A0004,A0001
因此,这个循环函数中缺少什么?
var imagedata = [{
id: "A0007",
class: "chair house grass"
},
{
id: "A0006",
class: "car house tree flower grass"
},
{
id: "A0005",
class: "car house tree flower"
},
{
id: "A0004",
class: "car tree"
},
{
id: "A0003",
class: "chair tree flower grass"
},
{
id: "A0002",
class: "chair house flower"
},
{
id: "A0001",
class: "car flower"
},
{
id: "A0000",
class: "chair car house tree flower grass"
}
];
function myFunction() {
var o2y = ["1", "all", "0", "1", "all", "0"];
var o3x = ["chair", "car", "house", "tree", "flower", "grass"];
var m0x = "";
var m1x = "";
for (i = 0; i < o3x.length; i++) {
m0x = o3x[i];
for (j = 0; j < imagedata.length; j++) {
if (o2y[i] == "0") {
if (imagedata[j].class.search(m0x) < 0) {
m1x += imagedata[j].id;
console.log(m1x);
}
}
};
};
}
myFunction();
最佳答案
尝试下面的代码,
var imagedata = [{
id: "A0007",
class: "chair house grass"
},
{
id: "A0006",
class: "car house tree flower grass"
},
{
id: "A0005",
class: "car house tree flower"
},
{
id: "A0004",
class: "car tree"
},
{
id: "A0003",
class: "chair tree flower grass"
},
{
id: "A0002",
class: "chair house flower"
},
{
id: "A0001",
class: "car flower"
},
{
id: "A0000",
class: "chair car house tree flower grass"
}
];
function myFunction() {
var o2y = ["1", "all", "0", "1", "all", "0"];
var o3x = ["chair", "car", "house", "tree", "flower", "grass"];
var m0x = "";
var m1x = "";
var index = [];
for (i = 0; i < o2y.length; i++)
if (o2y[i] == "0")
index.push(i);
for (j = 0; j < imagedata.length; j++) {
var flag = 0
for (k = 0; k < index.length; k++)
if (imagedata[j].class.search(o3x[index[k]]) < 0)
flag++
if(flag == index.length)
m1x += imagedata[j].id;
}
console.log(m1x);
}
myFunction();
关于javascript - 如何在循环函数中过滤已过滤的id来过滤另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343106/