'spread operator' 可以替换具有相同 uuid 的对象吗?解决方案:Object.assing()
Can the 'spread operator' replace an object with same uuid? Solution: Object.assing()
我试图用 spread syntax 替换数组中对象的 属性,如下所示:
const origArray = [
{
"uuid":"first-team-uuid",
"name":"Team 1",
"players": [
"first-team-uuid"
]
},
{
"uuid":"second-team-uuid",
"name":"Team 2",
"players":[]
}
]
const doesNotWork = (prev, index, newName) => [...prev, {...prev[index], name: newName}]
const result1 = doesNotWork(origArray, 0, "Team3")
console.log(result1)
// # I know this works:
const doesWork = (prev, index, newName) => {
let old = [...prev]
old.splice(index, 1, {...prev[index], name: newName});
return old;
}
const result2 = doesWork(origArray, 0, "Team3")
console.log(result2)
我希望reslut1 和result2 一样,但我好像错了。如果可能的话,我想用单行函数而不是我目前的解决方法来写这个。
我可以通过过滤器、地图和对象来推荐这些方法。
但是过滤方式改变了数组中元素的顺序
const origArray = [
{"uuid":"c752cf08","name":"Team 1",},
{"uuid":"d46829db","name":"Team 2",},
{"uuid":"d46829d0","name":"Team 3",}];
const match = 1;
const name = 'Team 100';
//------------------------
const workWithFilter = (prev) =>
[...prev.filter((_, i) => i !== match), { ...prev[match], name }];
const result1 = workWithFilter(origArray);
console.log('workWithFilter:', result1);
//------------------------
const workWithMap = (prev) =>
prev.map((v, i) => (i === match) ? { ...v, name } : v);
const result3 = workWithMap(origArray);
console.log('workWithMap:', result3);
//------------------------
const workWithObject = (prev) =>
Object.assign([...prev], { [match]: { ...prev[match], name } });
const result4 = workWithObject(origArray);
console.log('workWithObject:', result4);
//------------------------
const doesWork = (prev) => {
let old = [...prev]
old.splice(match, 1, { ...prev[match], name });
return old;
}
const result2 = doesWork(origArray);
console.log('doesWork:', result2);
.as-console-wrapper {max-height: 100% !important; top: 0}
我试图用 spread syntax 替换数组中对象的 属性,如下所示:
const origArray = [
{
"uuid":"first-team-uuid",
"name":"Team 1",
"players": [
"first-team-uuid"
]
},
{
"uuid":"second-team-uuid",
"name":"Team 2",
"players":[]
}
]
const doesNotWork = (prev, index, newName) => [...prev, {...prev[index], name: newName}]
const result1 = doesNotWork(origArray, 0, "Team3")
console.log(result1)
// # I know this works:
const doesWork = (prev, index, newName) => {
let old = [...prev]
old.splice(index, 1, {...prev[index], name: newName});
return old;
}
const result2 = doesWork(origArray, 0, "Team3")
console.log(result2)
我希望reslut1 和result2 一样,但我好像错了。如果可能的话,我想用单行函数而不是我目前的解决方法来写这个。
我可以通过过滤器、地图和对象来推荐这些方法。
但是过滤方式改变了数组中元素的顺序
const origArray = [
{"uuid":"c752cf08","name":"Team 1",},
{"uuid":"d46829db","name":"Team 2",},
{"uuid":"d46829d0","name":"Team 3",}];
const match = 1;
const name = 'Team 100';
//------------------------
const workWithFilter = (prev) =>
[...prev.filter((_, i) => i !== match), { ...prev[match], name }];
const result1 = workWithFilter(origArray);
console.log('workWithFilter:', result1);
//------------------------
const workWithMap = (prev) =>
prev.map((v, i) => (i === match) ? { ...v, name } : v);
const result3 = workWithMap(origArray);
console.log('workWithMap:', result3);
//------------------------
const workWithObject = (prev) =>
Object.assign([...prev], { [match]: { ...prev[match], name } });
const result4 = workWithObject(origArray);
console.log('workWithObject:', result4);
//------------------------
const doesWork = (prev) => {
let old = [...prev]
old.splice(match, 1, { ...prev[match], name });
return old;
}
const result2 = doesWork(origArray);
console.log('doesWork:', result2);
.as-console-wrapper {max-height: 100% !important; top: 0}