Basically I have an array of objects like
var array = [{x: 10, y: 5, r: 10, ...} ]
There are more fields in the object, but to the point, this is the way I've found to seperate the fields to arrays:
var x = array.map(obj => obj.x);
var y = array.map(obj => obj.y);
// and so on for each field
Works, but seems very inefficient as the number of fields and the array size grows, as this scans the array multiple times. I could use a standard loop, but I do prefer the map() as I think it is more readable in this case. Is it possible to return multiple values from map? I think something like:
var [x, y] = array.map(obj => [obj.x, obj.y]); // should output two arrays
Or anything similar which will be readable (reduce is less readable IMO).
- I did notice some newer javascript functions like
array.entries()
which return an iterator object. How can I implement such functions?