skip to Main Content

I have this array

var selectedId = [
  "12",
  "22"
];

var allData = [{
    "id": "12",
    "title": "Title 1",
    "description": "description"
  },
  {
    "id": "22",
    "title": "Title 2",
    "description": "description"
  },
  {
    "id": "25",
    "title": "Title 3",
    "description": "description"
  }
]

I want to check for multiple id using filter function

this.allData.filter((people => people.id=='12')); 

for single value it is working but i have both 12 and 22 and i want to get only these two records.

Any solution. Thanks

3

Answers


  1. You can use array.includes()

    const selectedId = [
      "12",
      "22"
    ];
    
    const allData = [
      {
        "id":"12",
        "title":"Title 1",
        "description":"description"
      },
      {
        "id":"22",
        "title":"Title 2",
        "description":"description"
      },
      {
        "id":"25",
        "title":"Title 3",
        "description":"description"
      }
    ]
    
    const filtered = allData.filter((people => selectedId.includes(people.id))); 
    console.log(filtered);
    Login or Signup to reply.
  2. var selectedId = [
      "12",
      "22"
    ];
    
    var allData = [{
        "id": "12",
        "title": "Title 1",
        "description": "description"
      },
      {
        "id": "22",
        "title": "Title 2",
        "description": "description"
      },
      {
        "id": "25",
        "title": "Title 3",
        "description": "description"
      }
    ];
    
    console.log(allData.filter(people => selectedId.includes(people.id)));
    Login or Signup to reply.
  3. You can apply this approach:-

    this.allData.filter((people => selectedId.includes(people.id)))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search