英文:
Update Array of Objects based on Array of strings
问题
我正在尝试更新一个对象数组。基于用户的选择或字符串数组,将active
键设置为true。
例如,选择ABC, DEF
后,这两个ID的active
键将设置为true
。
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
]
"ABC",
"DEF"
]
现在我需要基于默认数组创建一个新数组,并且只为selectedArray
中的字符串数组分配active=true
。
结果看起来像这样:
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": false,
"id": 3,
"name": "GHI",
"value": "3"
}
]
我尝试了以下方法。
arrCopy.forEach(x => x.active = false)
arrCopy.forEach(x => {
if(selectedArray.includes(x.name))
x.active = true
})
console.log(arrCopy)
上述代码有效,但是否有更标准的方法来实现相同的目标?谢谢!
英文:
I am trying to update an array of objects. The active
key will be set to true based on user's selection or the array of strings.
For an instance, on selecting ABC, DEF
, the active
key will be set to true
for these two Ids.
const defaultArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
]
const selectedArray = [
"ABC",
"DEF"
]
So now I need to create a new Array based on the default array and assign active=true
only for array of string in selecetedArray
.
Result will look like
const newArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": false,
"id": 3,
"name": "GHI",
"value": "3"
}
]
I tried.
const arrCopy = [...defaultArray]
arrCopy.forEach(x => x.active = false)
arrCopy.forEach(x => {
if(selectedArray.includes(x.name))
x.active = true
})
console.log(arrCopy)
The above code works, but is there any more standard way to achieve the same. thanks
答案1
得分: 5
你不需要两个循环来实现这个,你可以直接在defaultArray
上使用map
方法。
defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))
const defaultArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
]
const selectedArray = [
"ABC",
"DEF"
]
const newArray = defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))
console.log(newArray)
英文:
You don't need two loops to achieve that, you can directly use map
method on the defaultArray
.
defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const defaultArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
]
const selectedArray = [
"ABC",
"DEF"
]
const newArray = defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))
console.log(newArray)
<!-- end snippet -->
答案2
得分: 1
你可以简单地遍历你的对象数组,检查selectedArray是否包含每个对象的指定name属性,并将结果设置为active标志,如下所示 -
const defaultArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
];
const selectedArray = [
"ABC",
"DEF"
];
const arrCopy = [...defaultArray];
arrCopy.forEach(x => x.active = selectedArray.includes(x.name));
console.log(arrCopy);
英文:
You can simply iterate through your array of objects & see if selectedArray includes
the specified name
attribute from each object and set the result to active
flag, like the following -
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const defaultArray = [
{
"active": true,
"id": 1,
"name": "ABC",
"value": "1"
},
{
"active": true,
"id": 2,
"name": "DEF",
"value": "2"
},
{
"active": true,
"id": 3,
"name": "GHI",
"value": "3"
}
];
const selectedArray = [
"ABC",
"DEF"
];
const arrCopy = [...defaultArray];
arrCopy.forEach(x => x.active = selectedArray.includes(x.name));
console.log(arrCopy);
<!-- end snippet -->
答案3
得分: 0
首先,您可以删除这行代码:
arrCopy.forEach(x => x.active = false)
然后改成这样:
arrCopy.forEach(x => {
if (selectedArray.includes(x.name)) {
x.active = true;
} else {
x.active = false;
}
})
如果您的代码中会重复出现这段逻辑,您可以编写一个函数来实现这个目标。
英文:
first you can remove this line :
arrCopy.forEach(x => x.active = false)
and do it like this :
arrCopy.forEach(x => {
if(selectedArray.includes(x.name)){
x.active = true}
else{
x.active = false
}
})
you can write a function to do achieve that goal if this piece would be repeated in your code
答案4
得分: 0
将新参数添加到checkActive
函数中将检查对象中是否存在名称。
该函数如下:
function checkActive(...args){
return newArray.map(obj => {
if(args.includes(obj.name)){
obj.active = true
}else{
obj.active = false
}
return obj
})
}
console.log(checkActive("ABC","DEF"))
英文:
Adding new arguments to the checkActive
function will check in the object if the name exists.
The function is:
function checkActive(...args){
return newArray.map(obj=>{
if(args.includes(obj.name)){
obj.active = true
}else{
obj.active = false
}
return obj
})
}
console.log(checkActive("ABC","DEF"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论