【问题标题】:Object with nested array to filter, want to return entire object with new filtered array带有要过滤的嵌套数组的对象,想要返回带有新过滤数组的整个对象
【发布时间】: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


    【解决方案1】:

    如果要改变现有对象,只需将.filter 的结果分配给.actors 属性即可。

    movie.actors = movie.actors.filter((actor) => actor.id !== id);
    console.log(movie);
    

    如果您想保持现有对象不变,请在过滤时将其余属性分散到一个新对象中。

    const updatedMovie = {
      ...movie,
      actors: movie.actors.filter((actor) => actor.id !== id)
    };
    console.log(updatedMovie);
    

    【讨论】:

      【解决方案2】:

      const movies = {
          "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 id = 3; // or any other id you want
      const actors = movies.actors.filter(actor => actor.id != id)
      const newMovies = Object.assign({}, movies , { actors })
      console.log(newMovies)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-04
        • 1970-01-01
        • 2020-03-08
        • 1970-01-01
        • 2021-12-13
        • 2020-09-17
        • 1970-01-01
        相关资源
        最近更新 更多