how to map more than one property from array of object in javascript

时光毁灭记忆、已成空白 提交于 2020-04-02 07:49:07

问题


I have an array of Object as follows:

var obj = [
  {a: 1, b: 5, c: 9},
  {a: 2, b: 6, c: 10},
  {a: 3, b: 7, c: 11},
  {a: 4, b: 8, c: 12}
];

I know about how to get single object using Array.map() like this.

var result = obj.map(x=>x.a)

This will give me following result

[1, 2, 3, 4]

But I want result like follows:

[
  {a: 1, b: 5},
  {a: 2, b: 6},
  {a: 3, b: 7},
  {a: 4, b: 8}
]

inshort from array of object i want to select only few fields(more than one)

how to do that ?


回答1:


You can use .map() with Object Destructuring:

let data = [
    {a:1,b:5,c:9}, {a:2,b:6,c:10},
    {a:3,b:7,c:11}, {a:4,b:8,c:12}
];
          
let result = data.map(({ a, b }) => ({a, b}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }



回答2:


If, as in your example, you want to exclude a particular property or few, you can use destructuring and use rest properties to create an object with only the properties you want:

var obj = [
  {a:1,b:5,c:9},
  {a:2,b:6,c:10},
  {a:3,b:7,c:11},
  {a:4,b:8,c:12}
];
const mapped = obj.map(({ c, ...rest }) => rest);
console.log(mapped);

If you want to include properties, simply extract them from the .map callback:

var obj = [
  {a:1,b:5,c:9},
  {a:2,b:6,c:10},
  {a:3,b:7,c:11},
  {a:4,b:8,c:12}
];
const mapped = obj.map(({ a, b }) => ({ a, b }));
console.log(mapped);



回答3:


Use map():

var data = [
  {a:1,b:5,c:9},
  {a:2,b:6,c:10},
  {a:3,b:7,c:11},
  {a:4,b:8,c:12}
];
          
let modified = data.map(obj => ({a: obj.a, b: obj.b}))

console.log(modified);

Or if you prefer destructuring:

var data = [
    {a:1,b:5,c:9}, 
    {a:2,b:6,c:10},
    {a:3,b:7,c:11}, 
    {a:4,b:8,c:12}
];
          
let modified = data.map(({ a, b }) => ({a, b}));

console.log(modified);



回答4:


In your solution for producing [1,2,3,4], x.a isn’t some micro-syntax, it’s actually a full-fledged JavaScript expression. So you can just replace it with the JavaScript for creating a new object with the properties you want.

var result = obj.map(x=>{a: x.a, b: x.b});

... almost. The additional complication is that a { after a => is interpreted as the beginning of a function body, not the beginning of an object literal. You can avoid this by just wrapping the object literal in otherwise-noop parenthesis.

var result = obj.map(x=>({a: x.a, b: x.b}));



回答5:


You can return a custom object with required properties using map()

var obj = [{a:1,b:5,c:9},
           {a:2,b:6,c:10},
           {a:3,b:7,c:11},
           {a:4,b:8,c:12}
          ];
          
          
let op = obj.map(e => {
   return { a:e.a, b: e.b };
})        

console.log(op);



回答6:


You can make your own custom function for this, and pass it a set of properties which you want to extract. :

var array = [{a:1,b:5,c:9}, {a:2,b:6,c:10}, {a:3,b:7,c:11}, {a:4,b:8,c:12} ];

function extractProperties(arr, properties){
  return arr.map((obj)=> Object.keys(obj).reduce((acc,key)=>{
    if(properties.has(key))    
      acc[key] = obj[key];  
    return acc;
  },{}));
}
let set = new Set(["a","b"]);
let result = extractProperties(array, set);
console.log(result);

set.add("c");
console.log("**************result2**********")
let result2 = extractProperties(array, set);
console.log(result2);


来源:https://stackoverflow.com/questions/53718887/how-to-map-more-than-one-property-from-array-of-object-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!