猿问

在带有对象的嵌套数组中过滤

我有一个包含三个书籍对象的数组。每个书籍对象都有一个 bookIds 数组。我想以快速有效的方式按 bookId (3,1) 进行过滤,因为我的数组将来会很容易增长。我尝试使用 map,但即使使用 deepCopy 也会更改我的原始数组!有没有办法在不使用递归的情况下使用过滤器函数?


this.booksList = 

    [

      {

        "books": [

          {

            "bookId": 3

          },

          {

            "bookId": 2

          }

        ],

        "id": 1,

        "name": "Name 1",

        "description": "desc 1"

      },

      {

        "books": [

          {

            "bookId": 5

          },

          {

            "bookId": 2

          }

         ],

         "id": 2,

         "name": "Name 2",

         "description": "desc 2"

      },

      {

        "books": [

          {

            "bookId": 1

          },

          {

            "bookId": 3

          }

         ],

         "id": 3,

         "name": "Name 3",

         "description": "desc 3"

      }

    ]

地图方法:


let results = this.books.map(function (book) {

            book.books = book.books.filter(x => x.bookId == 1 || x.bookId == 3);

            return book;

        }).filter(({ books }) => books.length);

地图结果:不是预期的结果!


[

  {

    "books": [

      {

        "bookId": 3

      }

    ],

    "id": 1,

    "name": "Name 1",

    "description": "desc 1"

  },

  {

    "books": [

      {

        "bookId": 1

      },

      {

        "bookId": 3

      }

     ],

     "id": 3,

     "name": "Name 3",

     "description": "desc 3"

  }

]

预期成绩 :


[

  {

    "books": [

      {

        "bookId": 3

      },

      {

        "bookId": 2

      }

    ],

    "id": 1,

    "name": "Name 1",

    "description": "desc 1"

  },

  {

    "books": [

      {

        "bookId": 1

      },

      {

        "bookId": 3

      }

     ],

     "id": 3,

     "name": "Name 3",

     "description": "desc 3"

  }

]

谢谢,


喵喔喔
浏览 172回答 2
2回答

MMTTMM

我想你正在寻找filter并且some-const input =  [{books:[{bookId:3},{bookId:2}],id:1,name:"Name 1",description:"desc 1"},{books:[{bookId:5},{bookId:2}],id:2,name:"Name 2",description:"desc 2"},{books:[{bookId:1},{bookId:3}],id:3,name:"Name 3",description:"desc 3"}]const query =  [3, 1]const result =  input.filter(({ books = [] }) =>    books.some(({ bookId = null }) => query.includes(bookId))  )console.log(JSON.stringify(result, null, 2))输出 -[  {    "books": [      {        "bookId": 3      },      {        "bookId": 2      }    ],    "id": 1,    "name": "Name 1",    "description": "desc 1"  },  {    "books": [      {        "bookId": 1      },      {        "bookId": 3      }    ],    "id": 3,    "name": "Name 3",    "description": "desc 3"  }]

呼如林

const booksList=[  {books:[{bookId:3},{bookId:2}],id:1,name:"Name 1",description:"desc 1"},  {books:[{bookId:5},{bookId:2}],id:2,name:"Name 2",description:"desc 2"},  {books:[{bookId:1},{bookId:3}],id:3,name:"Name 3",description:"desc 3"},];const byBookIDs = (...IDs) =>  booksList.filter(b => b.books.some(b => IDs.includes(b.bookId)));console.log(byBookIDs(1, 3));MDN 数组过滤器- 返回一个子集MDN Array Some - 尽快匹配(并返回)一些MDN Array Includes - 在数组中查找项目
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答