"chapters":[
{
"_id": "5422c6ba0f6303ba22720e14",
"chapter_name": "Morbi imperdiet libero sed.",
"sub_title": "Nam id elit tincidunt amet.",
"mystatus": "noidea"
},
{
"_id": "5422bd0d5cb79ae6205b2f6a",
"chapter_name": "Donec id accumsan volutpat.",
"sub_title": "Cras volutpat velit nullam.",
"mystatus": "noidea"
},
{
"_id": "5423ea5f17c83ad41da1765e",
"chapter_name": "Donec id accumsan volutpat.",
"sub_title": "Cras volutpat velit nullam.",
"mystatus": "noidea"
}
],
一个数组,我有另外一个
"currentstat": [
"5423ea5f17c83ad41da1765e",
"5422c6ba0f6303ba22720e14",
"5422bd0d5cb79ae6205b2f6a"
],
我要检查在chapters._id
任何currentstat
数组值包含的内容。
我试过这样。
for (var i = 0; i < chapters.length; i++) {
var attenId = currentstat.toString();
if (attenId.indexOf(chapters[i]._id) != -1) {
}
}
我不能没有
VAR attenId = currentstat.toString得到的结果();
我还需要一件事。
我需要将这些status
数组值分配给相应的chapters
数组,根据currentstat
数组值。
status: [ 'passed', 'passed', 'attending']
整个代码是这样的
for (var i = 0; i < chapters.length; i++) {
var attenId = currentstat.toString();
if (attenId.indexOf(chapters[i]._id) != -1) {
allChapters[i].mystatus = (status[i]) ? status[i] : 0;
}
}
,但价值不分配给相应的_id
请帮帮忙!
使用'indexOf'很可能会比遍历每个迭代更好。 – 2014-09-25 11:24:01
你是对的。 – avidenic 2014-09-25 11:27:34
但是,您需要将其应用于不属于'currentstat'的'章节'... – Bergi 2014-09-25 11:30:53