You can use a mix of .map and the ... spread operator
You can set the value after you've created your new array
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {return {...a}})
array2.find(a => a.id == 2).name = "Not Two";
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Or you can do it in the .map
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {
var returnValue = {...a};
if (a.id == 2) {
returnValue.name = "Not Two";
}
return returnValue
})
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer from George on Stack OverflowUpdate Array containing objects using spread operator
javascript - Spread an array of objects into a parent object
Using spread operator on array of object to access elements
Spread operator with array and object
You can use a mix of .map and the ... spread operator
You can set the value after you've created your new array
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {return {...a}})
array2.find(a => a.id == 2).name = "Not Two";
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Or you can do it in the .map
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {
var returnValue = {...a};
if (a.id == 2) {
returnValue.name = "Not Two";
}
return returnValue
})
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Using Spred Operator, you can update particular array value using following method
let array = [
{ id: 1, name: "One" },
{ id: 2, name: "Two" },
{ id: 3, name: "Three" },
];
const label = "name";
const newValue = "Two Updated";
// Errow comes if index was string, so make sure it was integer
const index = 1; // second element,
const updatedArray = [
...array.slice(0, index),
{
// here update data value
...array[index],
[label]: newValue,
},
...array.slice(index + 1),
];
console.log(updatedArray);
You can use Object.assign() with spread syntax ...
const arrayOfObjects = [{
x: 'foo'
}, {
y: 'bar'
}];
const obj = {
hello: 'world'
};
var result = Object.assign({}, obj, ...arrayOfObjects);
console.log(result)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
U can use Object.assign();
Object.assign(obj, ...arrayOfObjects)
This option will mutate object obj instead of creating new instance.
You're probably looking for map
What you need is array.map(element => element.obj)
Here's what's wrong with your attempts:
console.log([...array].obj): You're spreading thearrayinto a new array, and then you're logging theobjproperty of the new array, which isundefined, as expectedconsole.log([...array.obj]): You're trying to spread theobjproperty ofarray, which is alsoundefined, meaning you're trying to spreadundefined, which throws you an error
Short answer, no.
We cannot use spread operator on array of object to access elements.
As @VLAZ said in a comment
Spreading doesn't work like mapping at all. So...no, you cannot make it work like mapping
Edit: This seems to be an issue since 2016, and apparently, no fix (yet? since 2016) because it seems like just an edge case.
Hi all, I accidentally mistyped [ with { at line 4 in the code below and it passes compiler check. Should this happen and why does it behave like that?
type Foo = number // just an example
let t: Foo[] = [] // [1,2,3]
let c: Foo[] = {...t}
console.log(c.map(e=>-e))It took me a few minutes in a sea of code to realise what's wrong. Needless to say, it was quite frustrating, I'm sorry if this is a stupid question.
playground link
here is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",