0

This is kinda a logic question. I tried hard, but just couldn't solve it!

For eg. I have an array: ["tech", "review", "howto"]. I have another huge array with id(s) and texts.

[
  { id: "tech", text: "Technology" },
  { id: "fin", text: "Finance" },
  { id: "digimark", text: "Digital Marketing" },
  { id: "coding", text: "Programming" },
  { id: "tutorial", text: "Tutorial" },
  { id: "howto", text: "How To" },
  { id: "writing", text: "Writing" },
  { id: "inspire", text: "Inspirational" },
  { id: "science", text: "Science" },
  { id: "politics", text: "Politics" },
  { id: "lifestyle", text: "Lifestyle" },
  { id: "food", text: "Food" },
  { id: "business", text: "Business" },
  { id: "entrepreneur", text: "Entrepreneurs" },
  { id: "history", text: "History" },
  { id: "health", text: "Health" },
  { id: "pet", text: "Pets" },
  { id: "parenthood", text: "Parenthood" },
  { id: "travel", text: "Travel" },
  { id: "india", text: "India" },
  { id: "china", text: "China" },
  { id: "uk", text: "United Kingdom" },
  { id: "us", text: "United States" },
  { id: "world", text: "World" },
  { id: "news", text: "News" },
  { id: "review", text: "Product Review" }
]

Using those assets, I want this response in javascript:

[
  { id: "tech", text: "Technology" },
  { id: "review", text: "Product Review" },
  { id: "howto", text: "How To" }
]

As per now, I am doing this

categorySorter(categories) {
    const categoryState = categorySuggessions.filter(category => category.id === categories.map(category => (category)))
    return categoryState
}

That returns me a blank (i.e. []) array.

What would you suggest?

NEWBIE HERE.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kumar Abhirup
  • 197
  • 4
  • 16

2 Answers2

2

You can use filter and includes

let arr = [{ id: "tech", text: "Technology" },{ id: "fin", text: "Finance" },{ id: "digimark", text: "Digital Marketing" },{ id: "coding", text: "Programming" },{ id: "tutorial", text: "Tutorial" },{ id: "howto", text: "How To" },{ id: "writing", text: "Writing" },{ id: "inspire", text: "Inspirational" },{ id: "science", text: "Science" },{ id: "politics", text: "Politics" },{ id: "lifestyle", text: "Lifestyle" },{ id: "food", text: "Food" },{ id: "business", text: "Business" },{ id: "entrepreneur", text: "Entrepreneurs" },{ id: "history", text: "History" },{ id: "health", text: "Health" },{ id: "pet", text: "Pets" },{ id: "parenthood", text: "Parenthood" },{ id: "travel", text: "Travel" },{ id: "india", text: "India" },{ id: "china", text: "China" },{ id: "uk", text: "United Kingdom" },{ id: "us", text: "United States" },{ id: "world", text: "World" },{ id: "news", text: "News" },{ id: "review", text: "Product Review" }]

let ids = ["tech", "review", "howto"]

let op = arr.filter(({id}) => ids.includes(id))

console.log(op)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

You were close, just use .indexOf to check if the key exists in your set or not ?

const data = [
  { id: "tech", text: "Technology" },
  { id: "fin", text: "Finance" },
  { id: "digimark", text: "Digital Marketing" },
  { id: "coding", text: "Programming" },
  { id: "tutorial", text: "Tutorial" },
  { id: "howto", text: "How To" },
  { id: "writing", text: "Writing" },
  { id: "inspire", text: "Inspirational" },
  { id: "science", text: "Science" },
  { id: "politics", text: "Politics" },
  { id: "lifestyle", text: "Lifestyle" },
  { id: "food", text: "Food" },
  { id: "business", text: "Business" },
  { id: "entrepreneur", text: "Entrepreneurs" },
  { id: "history", text: "History" },
  { id: "health", text: "Health" },
  { id: "pet", text: "Pets" },
  { id: "parenthood", text: "Parenthood" },
  { id: "travel", text: "Travel" },
  { id: "india", text: "India" },
  { id: "china", text: "China" },
  { id: "uk", text: "United Kingdom" },
  { id: "us", text: "United States" },
  { id: "world", text: "World" },
  { id: "news", text: "News" },
  { id: "review", text: "Product Review" }
];

const set = ["tech", "review", "howto"];

const result = data.filter(x => set.indexOf(x.id) !== -1);

console.log(result);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53