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.
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.
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.
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.