亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在帶有對象的嵌套數組中過濾

在帶有對象的嵌套數組中過濾

喵喔喔 2023-03-18 17:32:15
我有一個包含三個書籍對象的數組。每個書籍對象都有一個 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"  }]謝謝,
查看完整描述

2 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

我想你正在尋找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"

  }

]


查看完整回答
反對 回復 2023-03-18
?
呼如林

TA貢獻1798條經驗 獲得超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?- 在數組中查找項目


查看完整回答
反對 回復 2023-03-18
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號