-2

I have an array of objects containing objects with similar structure. I need to add a new key value pair to all the objects in the array. How can I do it without looping the entire array.

Providing an example of the output that I require,

let arr = [{a:1,b:2}, {a:2,b:3}];
let key = c;
let value = 3;

Required output will be like,

//arr2 = [ {a:1,b:2,c:3}, {a:2,b:3,c:3}]

P.S: Tried using spread operator with little or no success.

Dan Philip Bejoy
  • 4,321
  • 1
  • 18
  • 33
  • 4
    *How can I do it without looping the entire array.* Why do you have a requirement of **no looping**? – gurvinder372 Oct 26 '17 at 09:32
  • `arr[0].c = el.c; arr[1].c ...` – Teemu Oct 26 '17 at 09:32
  • @Teemu You'll still loop for all the indices, right? – Yash Oct 26 '17 at 09:33
  • @Yash Nope, I hardcode them to my file = ). – Teemu Oct 26 '17 at 09:34
  • @DanPhilip Can you please shed some light on this "no loop" thing? Why should you do things without proper tools? – Teemu Oct 26 '17 at 09:35
  • @T.J.Crowder The whole 'no-loop' policy is just to reduce overheads as I have to loop a huge array. Just checking if its possible. – Dan Philip Bejoy Oct 26 '17 at 09:36
  • FWIW, spread notation (it's not an operator) is not applicable to what you're trying to do. – T.J. Crowder Oct 26 '17 at 09:36
  • @Yash Hardly ... But I've to, since I've that stupid restriction. – Teemu Oct 26 '17 at 09:37
  • This added object, should it then, after adding, behave independently? If not, just keep a collection of shared objects?! – Yoshi Oct 26 '17 at 09:38
  • @DanPhilip If you are worried about complexity, it won't matter if you use a loop. Since you've to add a property to each of the object in the array, you've to at least visit the each one of them, making the complexity O(input). A simple `for` loop will be of same complexity. – abhishekkannojia Oct 26 '17 at 09:40
  • When you say loop, do you only mean in terms of a looping construct like while & for... or is Array.each, ok?. It's still a loop, but the loop construct has just been done for you. If so then it's as simple as `arr.forEach(e=>e.c=3)` – Keith Oct 26 '17 at 09:40
  • @Yoshi I just want the key and value to be added to each object of the array. – Dan Philip Bejoy Oct 26 '17 at 09:45
  • @Keith I was hoping to go with no loop at all, like a single point of access to all elements of the array. – Dan Philip Bejoy Oct 26 '17 at 09:46
  • I see, still, you could use a collection of *shared features* to look for undefined properties on first access. – Yoshi Oct 26 '17 at 09:46
  • @DanPhilip: *"The whole 'no-loop' policy is just to reduce overheads as I have to loop a huge array. Just checking if its possible."* Ah. :-) Nope, it isn't. (Don't do the `Object.prototype` thing, I was being facetious there.) – T.J. Crowder Oct 26 '17 at 10:11

2 Answers2

1

How can I do it without looping the entire array.

You can't (reasonably). The completely unreasonable way would be to add it to Object.prototype, which is inherited by all objects, and thus would add it to all objects in the array — and everywhere else.

But again: You can't reasonably, you need to loop (in one form or another). That loop might be recursion a'la functional programming, but it's basically still a loop.

Here's the recursive technically-not-a-loop way:

let arr = [{a:1,b:2}, {a:2,b:3}];
let el  = {c:3};
(function go(a, i, e) {
    if (i < a.length) {
        a[i].c = e.c;
        go(a, i + 1, e);
    }
})(arr, 0, el);
console.log(arr);
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Can you be more specific about adding a key value to the object prototype. – Dan Philip Bejoy Oct 26 '17 at 09:44
  • @DanPhilip You would just do `Object.prototype.c = 3` But that's not a good idea at all, but you could inherit from object, and then add to the inherited prototype. But then your array construct wouldn't look the same. – Keith Oct 26 '17 at 09:49
  • @DanPhilip: You **really, really, really don't want to do that**. But if you did, you wouldn't want to do it the way Keith showed. Instead: `Object.defineProperty(Object.prototype, "c", {value: el.c});` (so it's non-enumerable). But you **really, really, really don't want to do that**. :-) – T.J. Crowder Oct 26 '17 at 10:07
1

you can use map() operator on array and Object.assign() to append the key, value pair

 let arr = [{a:1,b:2}, {a:2,b:3}];
 let finalArray = arr.map(function(obj){return Object.assign(obj,{c:3});})
console.log(finalArray);