ES6 .filter within a .filter

后端 未结 2 1674
自闭症患者
自闭症患者 2021-01-15 01:01

So I have data like the following:

[
     {
      \"id\": 0,
      \"title\": \"happy dayys\",
      \"owner\": {\"id\": \"1\", \"username\": \"dillonraphael         


        
2条回答
  •  野的像风
    2021-01-15 02:05

    You don't want a filter inside a filter - rather, inside the filter, check if some of the tags objects have the .value property that you want

    const _moodboards = [
         {
          "id": 0,
          "title": "happy dayys",
          "owner": {"id": "1", "username": "dillonraphael"},
          "tags": [{"value": "Art", "label": "Art"}],
          "items": []
         },
         {
          "id": 1,
          "title": "happy dayys",
          "owner": {"id": "1", "username": "dillonraphael"},
          "tags": [{"value": "Architecture", "label": "Architecture"}],
          "items": []
         },
    ];
    const name = 'Architecture';
    console.log(_moodboards.filter(({ tags }) => (
      tags.some(({ value }) => value === name)
    )));

提交回复
热议问题