gpt4 book ai didi

javascript数组对象值包含另一个数组值

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:17 25 4
gpt4 key购买 nike

我有一个像这样的数组

"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();

我还需要一件事。

我需要根据currentstat数组值将这些status数组值分配给相应的chapters数组。

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请帮忙!

最佳答案

示例:

var currentstat = ["1","2","3"];
var chapters = [{_id:"2"}, {_id:"4"}, {_id:"6"}];

var objects = chapters.filter(function(chapter) {
return ~currentstat.indexOf(chapter._id);
});

只需将这些数组替换为您的数据,objects 将包含具有相同 ID 的对象(在此特定示例中为 [{_id:"2"}]

关于javascript数组对象值包含另一个数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com