Create a new array with data from an object array

Can anyone tell me the best way to create a new array that contains data from another array that is created as an object.

       totalPts.                              = [];
        driver	  				= [];
	driver[0]				= new Object();
	driver[0].pts			= 12;
	driver[0].race_pts	= 5;

        driver[1]				= new Object();
	driver[1].pts			= 4;
	driver[1].race_pts	= 8;

        driver[2]				= new Object();
	driver[2].pts			= 9;
	driver[2].race_pts	= 2;

From here I want to build a new array containing just the race_pts so it would totalPts would be [5,8,2];

Currently I am looping through the array and pushing each item to it but is there a better way to achieve this. I have red about map and reduce but not sure how this would be done.

1 Like

I would suggest you firstly reading more about map through this link : -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
and still if you were unable to process what needs to be done then you should look at the solution down here and try to understand it.

let newArray = driver.map(pts => (pts["race_pts"]))
console.log(newArray);

Here what is being done is, map gives us a new array after using a function on every element in the array we use map on. if you feel difficult to process what is pts => (pts["race_pts"]) then it is nothing but a function like this function something(pts) { return pts["race_pts"] } pts represents here the different objects in the driver array.

Hope you understood this clearly.

Thankyou very much for the reply - works brilliantly.
:+1: :+1: :+1:

1 Like

I have been messing around with mapping objects and the above response works fine but if if on each pts array I have another object (place) I have another array how do I map that and return an return a two dimensional array.

i.e, mappedArray[0]= [2,9,73]
mappedArray[1]=[2,4,5,9];

driver[1].pts[0].place = [2,9,7,3];

totalPts.                              = [];
        driver	  				= [];
	driver[0]				= new Object();
	driver[0].pts			= [];
        driver[0].pts[0]  = new Object();
       driver[0].pts[0].place = [2,9,7,3];
	driver[0].race_pts	= 5;

        driver[1]				= new Object();
	driver[1].pts			= [];
        driver[1].pts[0]  = new Object();
       driver[1].pts[0].place = [2,4,5,9];
	driver[1].race_pts	= 8;

        driver[2]				= new Object();
	driver[2].pts			= [];
        driver[2].pts[0]  = new Object();
       driver[2].pts[0].place = [1,3,8,10];
	driver[2].race_pts	= 2;

Thanks in advance

In that case you should use for loop because it will be much easier for you to understand it because before making any code efficient you need to know how it works. I would encourage you to firstly do it the for loop way then trying it out through map. In my opinion for loop will be more easy for any one to recognize what you really want to do and please don’t make a property named pts if you are mapping over using pts as argument because it creates confusion or you can try using something else while mapping over. If you still get stuck then let me know.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.