3

Have an array as

result1= [
    [{name:"a",fruit:"apple"},{name:"b",fruit:"banana"}],
    [{name:"o",fruit:"orange"}],
    [{name:"g",fruit:"grapes"},{name:"s",fruit:"strawberries"}]
]

and

result2 =[
    {country:"japan",color:"red"},
    {country:"NewZealand",colour:"yellow"},
    {country:"srilanka",colour:"green"}
]

would need to insert a property having arrays inside the second with the indexes being same...length of the two arrays are same...resulted array should be

result3 = [
    {country:"japan",color:"red",newproperty:"[{name:"a",fruit:"apple"},{name:"b",fruit:"banana"}]"},
    {country:"NewZealand",colour:"yellow",newproperty:"[{name:"o",fruit:"orange"}]"},
    {country:"srilanka",colour:"green",newproperty:"[{name:"g",fruit:"grapes"},{name:"s",fruit:"strawberries"}]"}
]

i have tried with the below code

result3 = (result2.map((o, i) => Object.assign(o, result1[i])))

result of this adding each object of array rather than whole array itself from result1 to result2

Have tried with the code to add a property using map result fluctuates...please help with this

4 Answers 4

3

you can use bracket notation to add new property to the object

const result1= [[{name:"a",fruit:"apple"},{name:"b",fruit:"banana"}],[{name:"o",fruit:"orange"}],[{name:"g",fruit:"grapes"},{name:"s",fruit:"strawberries"}]]

const result2 =[{country:"japan",color:"red"},{country:"NewZealand",colour:"yellow"},{country:"srilanka",colour:"green"}]

const result3 = result2.map((e,i) => {
  e['newproperty'] = result1[i];
  return e;
});

console.log(result3)

Sign up to request clarification or add additional context in comments.

1 Comment

It is worth to mentioned that the result2 has changed
2

You could need an object, not just the array for assigning.

result3 = result2.map((o, i) => Object.assign(    o,                result1[i]  )) // old
result3 = result2.map((o, i) => Object.assign({}, o, { newproperty: result1[i] })) // new
                                              ^^  ^  ^ ^^^^^^^^^^^             ^
                                              new object
                                                  source not target
                                                     object with
                                                        property

For preventing to mutate result2, you need an empty object as target object.

var result1 = [[{ name: "a", fruit: "apple" }, { name: "b", fruit: "banana" }], [{ name: "o", fruit: "orange" }], [{ name: "g", fruit: "grapes" }, { name: "s", fruit: "strawberries" }]],
    result2 = [{ country: "japan", color: "red" }, { country: "NewZealand", colour: "yellow" }, { country: "srilanka", colour: "green" }],
    result3 = result2.map((o, i) => Object.assign({}, o, { newproperty: result1[i] }));

console.log(result3);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

Not sure why you have included first parameter {} ?
@MaxZoom, without it updates result2, because that is how Object.assign works. if that is wanted, mapping is not required.
1

As per documentation

The Object.assign( target, ...sources ) method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

var result1= [[{name:"a",fruit:"apple"},{name:"b",fruit:"banana"}],[{name:"o",fruit:"orange"}],[{name:"g",fruit:"grapes"},{name:"s",fruit:"strawberries"}]];

var result2 =[{country:"japan",color:"red"},{country:"NewZealand",colour:"yellow"},{country:"srilanka",colour:"green"}];

var result3 = result2.map((o, i) => Object.assign({newproperty: result1[i]}, o));

console.log(result3);

Comments

0

I would use something like:

var result1 = [
  [{name:"a",fruit:"apple"},{name:"b",fruit:"banana"}],
  [{name:"o",fruit:"orange"}],
  [{name:"g",fruit:"grapes"},{name:"s",fruit:"strawberries"}]
];
var result2 =[
  {country:"japan",color:"red"},
  {country:"NewZealand",colour:"yellow"},
  {country:"srilanka",colour:"green"}
];

var result3 = result2.map( (elm, index) => {
  var newproperty = result1[index].map(elm => { return { ...elm } } );
  return { ...elm, newproperty};
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.