【发布时间】:2022-01-13 22:39:53
【问题描述】:
我正在尝试从嵌套在我的电影对象中的数组中删除一个对象,然后将其复制到一个新变量中。所以我原来的对象是这样的:
{
"id": 1,
"title": "Avatar",
"movieLength": 162,
"releaseDate": "2009-12-18",
"trailerUrl": "https://www.youtube.com/watch?v=5PSNL1qE6VY",
"genre": {
"id": 1,
"genre": "Action"
},
"rating": {
"id": 3,
"rating": "PG-13"
},
"director": {
"id": 1,
"lastName": "Cameron",
"firstName": "James"
},
"actors": [
{
"id": 2,
"lastName": "Worthington",
"firstName": "Sam"
},
{
"id": 3,
"lastName": "Weaver",
"firstName": "Sigourney"
},
{
"id": 4,
"lastName": "Saldana",
"firstName": "Zoe"
}
],
"comments": []
}
而我现在正在做的是
const updatedMovie = const updatedMovie = movie.actors.filter((actor) => actor.id !== id);
但如您所知,它只返回我过滤的演员数组。我想用新过滤的actor复制整个对象,这样对象就会像这样出来(删除actor id 3):
{
"id": 1,
"title": "Avatar",
"movieLength": 162,
"releaseDate": "2009-12-18",
"trailerUrl": "https://www.youtube.com/watch?v=5PSNL1qE6VY",
"genre": {
"id": 1,
"genre": "Action"
},
"rating": {
"id": 3,
"rating": "PG-13"
},
"director": {
"id": 1,
"lastName": "Cameron",
"firstName": "James"
},
"actors": [
{
"id": 2,
"lastName": "Worthington",
"firstName": "Sam"
},
{
"id": 4,
"lastName": "Saldana",
"firstName": "Zoe"
}
],
"comments": []
}
我已经尝试过四处阅读,但我没有任何运气可以找到任何与我一起工作的解决方案,所以如果有人可以帮助我或为我指出正确的方向,那就太好了!
【问题讨论】:
标签: javascript arrays javascript-objects