0

I am having an array of object like the following:

let data = [{
  name: "Product1",
  category: "category 1"
}, {
  name: "Product2",
  category: "category 2"
}, {
  name: "Product3",
  category: "category 3"
}, {
  name: "Product4",
  category: "category 4"
}]

var flattened = [].concat.apply([], data);

console.log(flattened)

// expected output:
// ["Product1", "Product2", "Product3", "Product4"]

See above what I tried. However, I would like to have the following expected output: ["Product1", "Product2", "Product3", "Product4"]

Any suggestions what I am doing wrong?

I appreciate your replies!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • You do not need to worry about these kind of utilities. You can use either underscore or loadash. For your issue, you can use underscore.js _.pluck method. It is just one line thing. You take any of these library you can use those utilities in entire project. – Amaranadh Meda Sep 04 '19 at 10:37

1 Answers1

2

Why not just simple map

let data = [{name: "Product1",category: "category 1"}, {name: "Product2",category: "category 2"}, {name: "Product3",category: "category 3"}, {name: "Product4",category: "category 4"}]

var flattened = data.map(({ name }) => name)

console.log(flattened)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60