-1

I have an array of objects that is fed by an external API e.g [{prop: val1}, {prop: val2}, {prop: val3}....]

I have to feed this object(my code) to a third-party library which expects the name of the property on the object to be 'xyz' instead of 'prop'.

What is the most efficient way (memory wise and faster) basically avoiding both: 1. iterating over the array 2. adding 'xyz' property to all objects in the array

to achieve this?

I am thinking along the lines of adding a getter for xyz to all objects that return the 'prop' value, but that does not save the looping. Adding the getting on the prototype level (Object.property) seems like a bad idea at this point.

Edit: I am not looking for different ways to loop through arrays in javascript like forEach or map. I have a very specific ask, and i am interested in exploring if it is at all possible to simply have a property proxy for 'xyz'.

necromancer
  • 19
  • 1
  • 3
  • Possible duplicate of [Add property to an array of objects](https://stackoverflow.com/questions/38922998/add-property-to-an-array-of-objects) – Luca De Nardi Nov 03 '17 at 09:40
  • @LucaDeNardi all the solutions there are looping through the array and adding the property. This is precisely what i want to avoid. – necromancer Nov 03 '17 at 09:42
  • Those are the only ways to do that. The fact that an array is a list of _multiple_ items require you to _loop_ through all the items – Luca De Nardi Nov 03 '17 at 09:43
  • If you want to avoid looping you can try something like `JSON.parse(JSON.stringify([{prop: 'x'}, {prop: 'y'}, {prop: 'z'}]).replace(/"prop"/,'"xyz"'));` but I still think looping is the proper way – dkasipovic Nov 03 '17 at 09:44
  • @DamirKasipovic the only reason to avoid looping is performance. replace will have even worse performance. – necromancer Nov 03 '17 at 09:51
  • @LucaDeNardi "Those are the only ways to do that." I am not convinced of this. Can you comment on the line of thinking i was talking about in my question. – necromancer Nov 03 '17 at 09:53
  • @necromancer there are no ways that I can think of to set multiple values without looping, as you noticed yourself with the getter option, and that's why I say that a loop is the only way. – Luca De Nardi Nov 03 '17 at 09:57
  • @LucaDeNardi fair enough. I will still keep this open as i am curious to know if anybody else can come up with something clever. – necromancer Nov 03 '17 at 10:00
  • @necromancer sure, that interests me as well, that would be a nice new thing I'll learn :) – Luca De Nardi Nov 03 '17 at 10:01

2 Answers2

0

Array map is used to cycle trough an array.

myArray.map(function(obj){
    obj.xyz = 'yourvalue';
    return obj;
}
Luca De Nardi
  • 2,280
  • 16
  • 35
  • That still gives an O(N) solution . Does it not ? – UchihaItachi Nov 03 '17 at 09:43
  • `Array.prototype.map()` is used to transform every element of an array and return those transformed values in a newly created array. – Andreas Nov 03 '17 at 09:44
  • This mutates each object in the original array, rather than returning a copy, also you don't assign the returned array to anything. Both of these facts point to using `forEach()` instead. – pilchard Sep 13 '21 at 09:03
0

You can use Array.map to create a new from the array you received by the API.

var newArray = oldArray.map(function(obj){
    return {newKey : obj.prop};
});

In this example, newKey will be the key property you want, instead of 'prop', and it's assigned the old 'prop' value

Ole Haugset
  • 3,709
  • 3
  • 23
  • 44