javascript - Putting another array as property into another array failed -
tried play spread of es6, didn't quite right
const arr1 = [{ gap: 10 }, { gap: 20 }, { gap: 30 }] const arr2 = [{ english: null }, { france: null }] how can produce new array, arr2's property become apart of arr1?
from jsfiddle:
//expected new_arr /*[{ gap: 10, english: null, france: null }, { gap: 20, english: null, france: null }, { gap: 30, english: null, france: null }]*/ failed attempt https://jsfiddle.net/swu94gd5/
you want
const arr1 = [{ gap: 10 }, { gap: 20 }, { gap: 30 }] const arr2 = [{ english: null }, { france: null }] const new_arr = arr1.map(obj => object.assign({}, obj, ...arr2)); console.log(new_arr); note:
i had
const new_arr = arr1.map(obj => object.assign(obj, ...arr2)); but changing arr1[0].gap change new_arr[0].gap - code in snippet fixes that
re comment can explain ur definition of object assign in ur head?
i don't have definition in head
i use definition in documentation
the object.assign() method used copy values of enumerable own properties 1 or more source objects target object. return target object.
object.assign(target, ...sources)
Comments
Post a Comment