gpt4 book ai didi

javascript - 如何查找对象数组中的属性并将这些对象移动到对象中的另一个数组?

转载 作者:行者123 更新时间:2023-12-01 00:56:05 28 4
gpt4 key购买 nike

如何查找对象中数组中的属性并将这些对象移动到对象中的另一个数组中?如何从 this.state.comments 中提取 {"comment": "value"} 并将其放入 comments 数组中的 comment 对象中在其他物体之后?我重新映射了数组并提取了值本身 - 注释属性。如何提取整个对象,使其看起来像这样 {"comment:"value "}

const comment = {
"comments": [{'comment': 'aaa'}, {'comment': 'bbb'}]
"description": " ytytyty"
"id": 3
"title": "gfgfgfgfgf"
}

this.state.comments = [
{"comment": "eeeee"},
{"comment": "rrrrr"},
{"comment": "ggggg"},
{"date: Thu Jun 13 2019 01:27:09
"desc": "dfdfdf"
"comment": "hhhhhh"
}
]

let v = this.state.comments.map(t => t.comment? t.comment : t);

console.log(`{comment: ${v}`);

预期效果:

const comment = {
"comments": [{'comment': 'aaa'}, {'comment': 'bbb'},
{"comment": "eeeee"}, {"comment": "rrrrr"}, {"comment":
"ggggg"}, "comment": "hhhhhh"]
"description": " ytytyty"
"id": 3
"title": "gfgfgfgfgf"
}

最佳答案

const comment = {
"comments": [{'comment': 'aaa'}, {'comment': 'bbb'}],
"description": " ytytyty",
"id": 3,
"title": "gfgfgfgfgf"
}

let newComments = [
{"comment": "eeeee"},
{"comment": "rrrrr"},
{"comment": "ggggg"},
{"date": "Thu Jun 13 2019 01:27:09",
"desc": "dfdfdf",
"comment": "hhhhhh"
}
]

newComments.forEach(t => {
if( t.comment ) comment.comments.push({
comment: t.comment
})
});

console.log(comment);

关于javascript - 如何查找对象数组中的属性并将这些对象移动到对象中的另一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56571904/

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