0

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',
    },
]
    
1
  • How does the expected output relate to the input? Commented Dec 3, 2020 at 16:53

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

it doesn't work correctly. it returns other objects that are together with the required ones in one array
@rise can you update your post with exact input and expected value?
0

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)

Comments

0

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);

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.