Javascript 修改一个对象';通过根据值选择属性来设置属性
我必须更改数组中对象属性的值。该数组如下所示:Javascript 修改一个对象';通过根据值选择属性来设置属性,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我必须更改数组中对象属性的值。该数组如下所示: let myArrWithObjs = [ { id: 1, selected: false }, { id: 2, selected: false }, { id: 3, selected: false } ]; changeObjectVal = (id) => { // here I need to first find the right object within the array //
let myArrWithObjs = [
{ id: 1, selected: false },
{ id: 2, selected: false },
{ id: 3, selected: false }
];
changeObjectVal = (id) => {
// here I need to first find the right object within the array
// and after that I should modify its aBoolean property to true
}
我有一个函数如下所示:
let myArrWithObjs = [
{ id: 1, selected: false },
{ id: 2, selected: false },
{ id: 3, selected: false }
];
changeObjectVal = (id) => {
// here I need to first find the right object within the array
// and after that I should modify its aBoolean property to true
}
因此,在函数中,我只知道id值。如果id参数的值为2
,我需要获取数组中的第二个对象(因为它的id
也是2)。之后,我需要更改其selected
属性的值
我发现这个代码:
var selectedChartItemObj = myArrWithObjs.filter(function(obj) {
return obj.id == id;
});
问题是,这给了我一个对象的副本,而不是对象本身
有没有办法以更短的方式修改正确对象的属性值?使用spread操作符,我可以确保不变异整个数组/对象,但首先我需要知道如何更改值。如果您不介意在数组中变异对象。这会奏效的
// Using Array.prototype.find to get the inner object
const obj = myArrayWithObjs.find(o => o.id === id);
// Set the selected value
obj.selected = true
如果您想避免任何突变:
// Get the index
const idx = myArrayWithObjs.findIndex(o => o.id === id);
const currObj = myArrayWithObjs[idx]
// New object with spread
const nextObj = {
...currObj,
selected: true
};
// New array with spread
const nextArray = [
...myArrayWithObjs.slice(0, idx),
nextObj,
...myArrayWithObjs.slice(idx + 1)
];
如果你不介意改变数组中的对象。这会奏效的
// Using Array.prototype.find to get the inner object
const obj = myArrayWithObjs.find(o => o.id === id);
// Set the selected value
obj.selected = true
如果您想避免任何突变:
// Get the index
const idx = myArrayWithObjs.findIndex(o => o.id === id);
const currObj = myArrayWithObjs[idx]
// New object with spread
const nextObj = {
...currObj,
selected: true
};
// New array with spread
const nextArray = [
...myArrayWithObjs.slice(0, idx),
nextObj,
...myArrayWithObjs.slice(idx + 1)
];
通过将其分配给另一个变量,可以创建其副本。如果您只是在过滤数组的第一项中设置属性,它将正常工作 如果有许多项具有相同的id,则可以使用.each循环将每个项的id设置为true
让myarrhithobjs=[
{id:1,selected:false},
{id:2,selected:false},
{id:3,selected:false}
];
changeObjectVal=(id)=>{
myarrhithobjs.filter(函数(obj){
返回obj.id==id;
})[0]。所选内容=true;
}
changeObjectVal(3);
console.log(myarrhithobjs)代码>通过将其分配给另一个变量,您正在创建它的副本。如果您只是在过滤数组的第一项中设置属性,它将正常工作
如果有许多项具有相同的id,则可以使用.each循环将每个项的id设置为true
让myarrhithobjs=[
{id:1,selected:false},
{id:2,selected:false},
{id:3,selected:false}
];
changeObjectVal=(id)=>{
myarrhithobjs.filter(函数(obj){
返回obj.id==id;
})[0]。所选内容=true;
}
changeObjectVal(3);
console.log(myarrhithobjs)代码>我建议您使用changeObjectVal
函数的以下简单实现:
changeObjectVal = (id) => {
// here I need to first find the right object within the array
// and after that I should modify its aBoolean property to true
for(obj of myArrWithObjs) {
if(id === obj.id) {
obj.selected = true;
break;
}
}
}
我建议您使用changeObjectVal
函数的以下简单实现:
changeObjectVal = (id) => {
// here I need to first find the right object within the array
// and after that I should modify its aBoolean property to true
for(obj of myArrWithObjs) {
if(id === obj.id) {
obj.selected = true;
break;
}
}
}
您可以在找到对象后使用。此方案使用默认对象,但如果所有具有所需id
的对象都存在,则可以忽略该对象
Object.assign(array.find(o => o.id === id) || {}, { selected: true });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ find object
// ^^^^^^^^^^^^^^^^^^ update value
您可以在找到对象后使用。此方案使用默认对象,但如果所有具有所需id
的对象都存在,则可以忽略该对象
Object.assign(array.find(o => o.id === id) || {}, { selected: true });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ find object
// ^^^^^^^^^^^^^^^^^^ update value
您可以使用来迭代和更改对象:
const myarrhithobjs=[
{id:1,selected:false},
{id:2,selected:false},
{id:3,selected:false}
];
myarrhithobjs.forEach((o)=>o.id==2&&(o.selected=true));
console.log(myarrhithobjs)代码>可用于迭代和更改对象:
const myarrhithobjs=[
{id:1,selected:false},
{id:2,selected:false},
{id:3,selected:false}
];
myarrhithobjs.forEach((o)=>o.id==2&&(o.selected=true));
console.log(myarrhithobjs)代码>id
值是否唯一?id
值是否唯一?