V
V
Vadim Kinev2021-07-13 18:59:13
JavaScript
Vadim Kinev, 2021-07-13 18:59:13

Filtering an array for matches?

let arr1 = [ ["british", "classic-rock"], ["pop"], ["rock", "classic-rock"] ]

let arr2 = ["british", "pop"]


How to filter arr1 for arr2 matches?

The output should be -[ ["british", "classic-rock"], ["pop"] ]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WbICHA, 2021-07-13
@WblCHA

const groups = [
    ["british", "classic-rock"],
    ["pop"],
    ["rock", "classic-rock"],
  ];
  const filterKeys = ["british", "pop"];
  
  groups.filter((g) => filterKeys.some((k) => g.includes(k)))

A
Anton Neverov, 2021-07-13
@TTATPuOT

Understood nothing. Why did you take classic-rock into the final array in the first example , and decided to discard it in the second?
Most likely, the .filter() method will suit you . You simply check through this method each element of the array for the presence in the selected ones.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question