1

How do I group the array via javascript to just

  • Vehicles
  • Food

I can't figure it out via "reduce" since the array has no "key" to specify.

enter image description here

Thank you

Hossein Mousavi
  • 3,118
  • 3
  • 16
  • 35

2 Answers2

1

I think I don't get your question completely, but if you are trying to remove duplicates, this could be an option

const originalArray = ["vehicle", "food", "vehicle", "food", "vehicle", "food"];
const noDuplicates = new Set(originalArray);
console.log(Array.from(noDuplicates));
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
Franco Aguilera
  • 617
  • 1
  • 8
  • 25
1
const group1 = arr.filter(item => item === 'Vehicles');
const group2 = arr.filter(item => item === 'Food');

Or if you want a general solution:

let groups = {};
for (const item of arr) {
  if (!groups[item]) groups[item] = [];
  groups[item].push(item);
}
Andrey Bessonov
  • 338
  • 1
  • 7