-1
const data = [
  { name: "name1", option: "option1", category: [{ id: 1, value: "national" }] },
  { name: "name2", option: "option2", category: [{ id: 2, value: "international" }] },
  { name: "name3", option: "option3", category: [{ id: 3, value: "sports" }] },
  { name: "name4", option: "option4", category: [{ id: 4, value: "entertainment" }] },
];

I would like to filter of this array value of category // // console.log(data);

const result = data.filter(e => {
  return e.category.filter(b => b.value === 'national')
})

// I want output like bellow

{ name: "name1", option: "option1", category: [{ id: 1, value: "national" }] },

4
  • What's the final result you are looking for and what have you tried that is not working ? Commented Jun 23, 2020 at 14:56
  • Please show example of expected output. Commented Jun 23, 2020 at 14:56
  • I have change my question plz Commented Jun 23, 2020 at 15:09
  • data.filter(e => e.category.some(b => b.value === 'national'))…? Keeps all data items that have any 'national' value in category. Commented Jun 23, 2020 at 15:14

1 Answer 1

0

I modify the code for multiple arrays in category:

const data = [
  { name: "name1", option: "option1", category: [{ id: 1, value: "national" }, {id: 1, value: "mundial" }]},
  { name: "name2", option: "option2", category: [{ id: 2, value: "international" }] },
  { name: "name3", option: "option3", category: [{ id: 3, value: "sports" }] },
  { name: "name4", option: "option4", category: [{ id: 4, value: "entertainment" }] },
];

var result = data.filter(x => x.category.some(y => y.value == "mundial"));
console.log(result);

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

2 Comments

But i can have multiple national value
i modify the code, maybe this is what you are looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.