Alternative of Object.assign(...array)
Asked Answered
N

4

35

Assume we have array of objects.

Calling Object.assign(...array) makes an inheritance among those objects where object with index i override existing properties in object with index i-1

For example:

var array=[{interf:'IPerson',name:'Someone'},{clss:'Person',name:'Ahmed'},{student:true}];
console.log(
    Object.assign(...array) // Object.assign(array[0],array[1],array[2])
)

Now, using Babel with the proposed object spread syntax, we can do this statically :

{...array[0],...array[1],...array[2]} // spread used for each object not for array

How to do that dynamically?

There is overlap of context of "spread syntax". I mean how to use spread syntax for both:

  • For the Array to spread elements.
  • For the output literal object {} to make inheritance

?

I tried {...array} and it returns {0:<array[0]>,1:<array[1]>,2:<array[2]>} which is not the same output as Object.assign(...array).

Neurosis answered 16/8, 2016 at 22:47 Comment(5)
ES7 is done and released, object spread is not part of it. Just say "proposed object spread syntax", don't say ES7.Antitoxic
Assignment has nothing to do with inheritance. Use the terms "extend" or "mixin".Hothead
@loganfsmyth: Do you think we should have a tag for each of those proposals? (created on demand)Howbeit
Tough one, I don't know if anyone would use them, but could be niceAntitoxic
can somebody suggest an alternative for this with a snippet?Fixing
H
49

You are looking for

var obj = Object.assign({}, ...array)

that creates a new object instead of mutating array[0].

Hothead answered 16/8, 2016 at 22:54 Comment(4)
Would you happen to have an IE11 safe alternative for .assign() laying around? Looking for that now because of this (See requirements)Laveta
No, you can safely use Object.assign in IE11, just polyfill itHothead
would you try with: let array = [1, {a:1,b:2}, [3,4,5]]; the result isn't trivialAtaliah
@Ataliah OP has an array of objects. It doesn't make sense to use this code for arrays containing other arrays or numbers. What do you mean by "trivial", what output did you expect?Hothead
T
2

Seems to me the method you're looking for is .concat()

Returns a new array which is a merge of the target and the source.

Theophylline answered 11/3, 2019 at 14:41 Comment(0)
F
0

I thing the method you are looking for, as mentioned above, is .concat(), which creates a copy of an array and you could extend it without changing the original.


const a = [{ name: 'Bob', age: 36 }, { name: 'Villy', age: 23 }];

// ES5 concat method
const b = [].concat(a);

// ES6 shorthand alternative
const c = [...a];

c.push({ name: 'Peter', age: 19 });

b.push({ name: 'Anna', age: 27 });

// as a result you have three different arrays

/* a => [{name: 'Bob', age: 36}, 
{name: 'Villy', age: 23} */

/* b => [{name: 'Bob', age: 36}, 
{name: 'Villy', age: 23},
{name: 'Anna', age: 27}] */

/* c => [{name: 'Bob', age: 36}, 
{name: 'Villy', age: 23},
{name: 'Peter', age: 19}] */ 

Featherweight answered 30/10, 2019 at 8:37 Comment(0)
F
-1

Assign Objects with array object:

var a = {
  'name': 'Ajay'
}

var b = {
  approvers: [{
    id: '12'
  }]
}

var c = Object.assign(a, b);
console.log(c);
Fortunate answered 27/7, 2020 at 10:27 Comment(1)
The variable a and b are type of an object and not an arrayUnlace

© 2022 - 2024 — McMap. All rights reserved.