ecmascript 6 - Fetch multiple properties from an array of objects and form a new one : Javascript - Stack Overflow

Been trying to do the following thing:I have an array of objects ,var arr = [{ key: "aabFaa",

Been trying to do the following thing:

I have an array of objects ,


var arr = [
  { key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
  { key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];

Would want to fetch the "text" and "field" out of it and form a new array of objects something like this:

result = [ { "field" : "firstName" , value : "aabFaa" , type :"add"}, 
           { "field" : "firstName" , value : "aAaaaa" , type: "add"}
         ]  

Here type is hard coded one, where as rest are fetched from the "arr"

Whats the easier way to do this?

Have tried this:

var arr = [
  { key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
  { key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];

let result = arr.map(a => a.text); 
console.log(result)

Been trying to do the following thing:

I have an array of objects ,


var arr = [
  { key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
  { key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];

Would want to fetch the "text" and "field" out of it and form a new array of objects something like this:

result = [ { "field" : "firstName" , value : "aabFaa" , type :"add"}, 
           { "field" : "firstName" , value : "aAaaaa" , type: "add"}
         ]  

Here type is hard coded one, where as rest are fetched from the "arr"

Whats the easier way to do this?

Have tried this:

var arr = [
  { key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
  { key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];

let result = arr.map(a => a.text); 
console.log(result)

But this has to be written in multiple lines to get desired properties.Is there an easier approach?

Share Improve this question edited Jul 16, 2019 at 12:49 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jul 16, 2019 at 12:40 joy08joy08 9,6629 gold badges42 silver badges81 bronze badges 1
  • 1 Curious as to what you mean by "easier way" - what's easier than mapping over each item and returning the object with properties you need? "has to be written in multiple lines" Do you really need a one-liner for this? – chazsolo Commented Jul 16, 2019 at 12:43
Add a ment  | 

3 Answers 3

Reset to default 5

use map with Object Destructuring.

var arr = [
    { key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
    { key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
  ];

const output = arr.map(({field, text}) => ({field, value: text, type: "add"}));

console.log(output);

Using map seems like a good approach, but you would return a new object and not just one property:

let result = arr.map(a => ({value: a.text, type: 'add', field: a.field}));
let result = arr.map(obj => ({
    field: obj.field,
    value: obj.text,
    type: "add"
}));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745255023a4618902.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信