ketan pradhan ketan pradhan - 3 months ago 16
AngularJS Question

Convert array of objects to array of strings in angularjs

[
{ "text": "demo1" },
{ "text": "demo2" }
]


to

["demo1", "demo2"]


I have tried using reduce()

str str
Answer

You can use Array.prototype.map for that:

var arr = [
  {"text":"demo1"},
  {"text":"demo2"}
];
var texts = arr.map(function(el) {
  return el.text;
});
console.log(texts);

And with ES6, you can use arrow functions:

var texts = arr.map((el) => el.text);
Comments