简体   繁体   English

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

[英]Filter inside a nested array with objects

I have an array that contains three book objects.我有一个包含三个书对象的数组。 Each book object has an array of bookIds .每个 book 对象都有一个 bookIds 数组。 I want to filter by bookId (3,1) in a fast and efficient way since my array will grow up easly in the future.我想以一种快速有效的方式按 bookId (3,1) 过滤,因为我的数组将来会很容易增长。 I tried with map, but it change my original array even with a deepCopy !我尝试使用地图,但即使使用 deepCopy 它也会更改我的原始数组! Is there a way to use the filter function instead without using recursivity ?有没有办法在不使用递归的情况下使用过滤器函数?

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"
      }
    ]

The map approach :地图方法:

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

Result with map : not expected result !结果与地图:不是预期的结果!

[
  {
    "books": [
      {
        "bookId": 3
      }
    ],
    "id": 1,
    "name": "Name 1",
    "description": "desc 1"
  },
  {
    "books": [
      {
        "bookId": 1
      },
      {
        "bookId": 3
      }
     ],
     "id": 3,
     "name": "Name 3",
     "description": "desc 3"
  }
]

expected results :预期成绩 :

[
  {
    "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"
  }
]

Thanks,谢谢,

I think you are looking for filter and some -我想你正在寻找filtersome -

 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))

Output -输出 -

[
  {
    "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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM