Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}
]

In short, from an array of objects I want to select only a few fields (more than one).

How can I do that?

like image 493
user144271 Avatar asked Dec 11 '18 06:12

user144271


People also ask

How do you map an array of objects?

The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.

How do you add properties to an array of objects?

We can use the forEach method to loop through each element in an object and add a property to each. We have the arr array. Then we call forEach with a callback that has the element parameter with the object being iterated through and we assign the b property to a value. according to the console log.

How many properties can a JavaScript object have?

JavaScript objects have two types of properties: data properties and accessor properties.

How do I map multiple arrays?

To map multiple arrays with JavaScript, we can use the map method. to define the zip function that calls a1. map to combine the entries from a1 and a2 with index i into one entry. Then we call zip with arr1 and arr2 to zip them into one array.

How to map multiple properties from array of objects in JavaScript?

1. Map multiple properties from array of objects JavaScript The array.map () method is iterated over the element of the array and modifies elements of the array by applying some operation and returning the modified element as a new array.

What does map () do in JavaScript?

It creates a new array without modifying the elements of the original array. In this tutorial, I will show you how to use this map method with an array of objects with an example.

How do you map an array in JavaScript?

JavaScript map with an array of objects JavaScript map method is used to call a function on each element of an array to create a different array based on the outputs of the function. It creates a new array without modifying the elements of the original array.

How to add a new property to an array of objects?

In order to add a new property to every object from the array of objects, the transformed object with a new property value has to be returned in the Array.map () callback function. We use the JavaScript spread operator to include all existing object property values along with the new properties that need to be added to object entries.


2 Answers

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; }
like image 135
Mohammad Usman Avatar answered Oct 19 '22 17:10

Mohammad Usman


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);
like image 11
CertainPerformance Avatar answered Oct 19 '22 18:10

CertainPerformance