rise
rise

Reputation: 27

filter array by value based on another array

I have an array of cards. And I need to filter it by value word based on another array. only those objects should remain in cards that correspond to the names in the array difficultWords

const cards = [ 
  {
      word: 'cry',
    },
    {
      word: 'fishing',
    },
    {
      word: 'fly',
    },
    {
      word: 'hug',
    },
  ],
  [
    {
      word: 'open',
    },
    {
      word: 'play',
    },
    {
      word: 'run',
    },
    {
      word: 'sing',
    },
  ],
.....
let difficultWords = ["cry", "run", "sing"];

That's my code: (but id doesn't work)

 let wrongCard = cards.forEach(card => {
    card.filter(el => difficultWords.forEach(i => el.word === i));
 }

The output should be

let wrongCard = [ 
  [
    {
      word: 'cry',
    },
  ],
  [
    {
      word: 'run',
    },
    {
      word: 'sing',
    },
]
    

Upvotes: 0

Views: 46

Answers (4)

Sathish Kumar
Sathish Kumar

Reputation: 34

It works, Congrats!!!!

const cards = [
  [{ word: "hug" }],
  [{ word: "open" }, { word: "play" }, { word: "hug" }],
  [{ word: "point" }],
];
let difficultWords = ["point", "hug", "frog", "lion"];

let res = [];
difficultWords.forEach(val => {
  cards.map(item => {
  item.map(e =>{
  if(e.word === val) {
   res.push(e)
  }})
  })
})

console.log(res)

Upvotes: 0

michael
michael

Reputation: 4173

const cards = [
  [
    {
      word: 'cry'
    },
    {
      word: 'fishing'
    },
    {
      word: 'fly'
    },
    {
      word: 'hug'
    }
  ],
  [
    {
      word: 'open'
    },
    {
      word: 'play'
    },
    {
      word: 'run'
    },
    {
      word: 'sing'
    }
  ]
];
let difficultWords = ['cry', 'run', 'sing'];

const wrongCard = cards.map((card) =>
  card.filter((c) => difficultWords.includes(c.word))
);

console.log(wrongCard);

Upvotes: 1

Unmitigated
Unmitigated

Reputation: 89497

You can use Array#map to create a new array with the filtered values.

const cards = [ 
  [{
      word: 'cry',
    },
    {
      word: 'fishing',
    },
    {
      word: 'fly',
    },
    {
      word: 'hug',
    },
  ],
  [
    {
      word: 'open',
    },
    {
      word: 'play',
    },
    {
      word: 'run',
    },
    {
      word: 'sing',
    },
  ]];
let difficultWords = ["cry", "run", "sing"];
const res = cards.map(x => x.filter(({word}) => difficultWords.includes(word)));
console.log(res);

Upvotes: 0

Lu Kevin
Lu Kevin

Reputation: 23

you can use .map and .includes for this.

let wrongCard = cards.forEach(card => {
    card.filter(el => difficultWords.forEach(i => el.word === i));
 }

because you write let wrongCard = cards.forEach..., so I assumed that you wan to assign the result to to wrongCard.

to transform an array to another array, .map should be used, because .forEach will return undefined.

next, because difficultWords is array of string, and the word also is string, to check a value is inside an array, we can use diffultWords.includes(word), this will return true is the word is contain in the diffultWords.

const cards = [
  [{ word: "hug" }],
  [{ word: "open" }, { word: "play" }, { word: "hug" }],
  [{ word: "point" }],
];
let difficultWords = ["point", "hug", "frog", "lion"];

const result = cards.map((arr) => {
  return arr.filter((value) => {
    return difficultWords.includes(value.word);
  });
});

console.log(result)

Upvotes: 0

Related Questions