How do I filter a typescript array with items in another array

Anand

I have 2 arrays:

 [
  {
    "id": 1,
    "name": "All",
  },
  {
    "id": 2,
    "name": "APR",
  },
 {
    "id": 3,
    "name": "TER",
  }]

The second array is ["APR", "TER"]

I want to filter the first array with the second that is the output should be

  [{
    "id": 2,
    "name": "APR",
  },{
    "id": 3,
    "name": "TER",
  }]

Trying this with filter function - is that possible?

Thanks Anand

CRice

Pretty standard use of the filter method. Just give it the right condition to check, and you're good to go:

const myArray = [{
  "id": 1,
  "name": "All",
}, {
  "id": 2,
  "name": "APR",
}, {
  "id": 3,
  "name": "TER",
}];

const otherArray = [
  "APR",
  "TER",
];

const filtered = myArray.filter(x => otherArray.includes(x.name));

console.log(filtered)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I push individual array items into another array with PHP?

From Dev

How do I remove/filter specific items from an array?

From Dev

How do I filter multiple items from an array and add to an object?

From Java

How do I filter an array with TypeScript in Angular 2?

From Dev

How do I filter a matrix in R by matching another array

From Java

How to filter array based on items being in another array?

From Dev

How do I filter an array of data by id or name using another array of data to display in React Table?

From Dev

how to filter array in typescript

From Dev

How to filter Array with another array

From Java

How do I remove an array item in TypeScript?

From Dev

In TypeScript how do I access the member of an array?

From Dev

How Do I Separate an Array in Typescript

From Dev

How do I arrange an array in terms of another?

From Dev

How do I determine the number of items I have stored in an array?

From Dev

How do I fill a column of an array with the contents of another array?

From Dev

How do I sort one array by the corresponding values in another array?

From Dev

How do I push unique copies of array into another array?

From Dev

How do I find elements in array with another indexes array?

From Dev

How do I fill a column of an array with the contents of another array?

From Dev

How do I build and compare an Array against another array?

From Dev

How do I insert a multidimensional array into another array in a loop?

From Dev

In Javascript how do I divide a multidimensional array by another array

From Dev

How do I add the elements of an array to another array

From Dev

How do I make each each element in array another array?

From Dev

How do I store an array of strings into another array of strings?

From Dev

How do I put specific elements from an array into another array

From Dev

Filter Array Not in Another Array

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

How do I count the number of items in an array that start with the highest number?

Related Related

  1. 1

    How do I push individual array items into another array with PHP?

  2. 2

    How do I remove/filter specific items from an array?

  3. 3

    How do I filter multiple items from an array and add to an object?

  4. 4

    How do I filter an array with TypeScript in Angular 2?

  5. 5

    How do I filter a matrix in R by matching another array

  6. 6

    How to filter array based on items being in another array?

  7. 7

    How do I filter an array of data by id or name using another array of data to display in React Table?

  8. 8

    how to filter array in typescript

  9. 9

    How to filter Array with another array

  10. 10

    How do I remove an array item in TypeScript?

  11. 11

    In TypeScript how do I access the member of an array?

  12. 12

    How Do I Separate an Array in Typescript

  13. 13

    How do I arrange an array in terms of another?

  14. 14

    How do I determine the number of items I have stored in an array?

  15. 15

    How do I fill a column of an array with the contents of another array?

  16. 16

    How do I sort one array by the corresponding values in another array?

  17. 17

    How do I push unique copies of array into another array?

  18. 18

    How do I find elements in array with another indexes array?

  19. 19

    How do I fill a column of an array with the contents of another array?

  20. 20

    How do I build and compare an Array against another array?

  21. 21

    How do I insert a multidimensional array into another array in a loop?

  22. 22

    In Javascript how do I divide a multidimensional array by another array

  23. 23

    How do I add the elements of an array to another array

  24. 24

    How do I make each each element in array another array?

  25. 25

    How do I store an array of strings into another array of strings?

  26. 26

    How do I put specific elements from an array into another array

  27. 27

    Filter Array Not in Another Array

  28. 28

    How do I nest an array of items dividing them by their first value?

  29. 29

    How do I count the number of items in an array that start with the highest number?

HotTag

Archive