英文:
JavaScript find property match with in formcontrol object
问题
你好,以下是代码的翻译部分:
// 我有一个对象数组,正在对其进行循环
let DOArr: [] =[]
{
"selectedDC": {
"DC": [{
"id": "23293839",
"name": "Legal · L5",
"active": true,
"parentid": "23293827",
"level": 4
},
{
"id": "23293839",
"name": "Balance · L2",
"active": true,
"parentid": "23293807",
"level": 2
}
]
}
}
for (const tmpDataOwner of this.dataConceptDetailsObj['selectedDC']['DC']) {
this.DOArr.push(tmpDataOwner.id);
}
现在,我已经填充了复选框表单控件,它们的默认值为false。
我想要将那些 id
和 name
与上面匹配的复选框表单控件设置为true/已选中。
const formControls = this.dataOwnerList.map(
(control) => {
if(this.DOArr.includes(control.id)){
return new FormControl(true);
}
else{
return new FormControl(false);
}
}
);
现在,我只是检查 id
,但我也想检查 name
,基本上需要强力检查 id
和 name
的匹配。
英文:
Hi I've an array of object which I'm looping
let DOArr: [] =[]
{
"selectedDC": {
"DC": [{
"id": "23293839",
"name": "Legal · L5",
"active": true,
"parentid": "23293827",
"level": 4
},
{
"id": "23293839",
"name": "Balance · L2",
"active": true,
"parentid": "23293807",
"level": 2
}
]
}
}
for (const tmpDataOwner of this.dataConceptDetailsObj['selectedDC']['DC']) {
this.DOArr.push(tmpDataOwner.id)
}
Now I've so already populated checkbox formcontrols which I've selected default value as false.
I want to make those checkbox formcontrol true/checked whose id
and name
matches the above
const formControls = this.dataOwnerList.map(
(control) => {
if(this.DOArr.includes(control.id)){
return new FormControl(true);
}
else{
return new FormControl(false);
}
}
);
Now I'm only checking for id but I also want to check for name basically need strong checking for both id
& name
答案1
得分: 0
首先,我建议在代码中去掉for
循环,而是将dataConceptDetailsObj
直接映射到DOArr
,因为这样更省力。然后,不要单独推送id
,而是返回一个属性包,其中包含id
和name
键,它们的值与当前项对应。
let dataConceptDetailsObj = {
"selectedDC": {
"DC": [{
"id": "23293839",
"name": "Legal · L5",
"active": true,
"parentid": "23293827",
"level": 4
}, {
"id": "23293839",
"name": "Balance · L2",
"active": true,
"parentid": "23293807",
"level": 2
}]
}
}
let DOArr = dataConceptDetailsObj['selectedDC']['DC']
.map((item) => {
return {
id: item.id,
name: item.name
}
})
对于第二部分:
使用DOArr
数组的find
方法,而不是includes
,这样可以同时检查id
和name
。
find()
方法返回提供的数组中第一个满足提供的测试函数的元素。如果没有满足测试函数的值,则返回undefined
。 -MDN
const formControls = this.dataOwnerList.map(
(control) => {
let item = this.DOArr.find(
(i) => i.id == control.id && i.name == control.name
);
return new FormControl((item !== undefined));
}
);
英文:
First, I propose dropping the for
in favor of mapping dataConceptDetailsObj
directly into DOArr
since it's less work. Then instead of pushing the id
on its lonesome, return a property bag with id
and name
keys whose values correspond to the current item.
let dataConceptDetailsObj = {
"selectedDC": {
"DC": [{
"id": "23293839",
"name": "Legal · L5",
"active": true,
"parentid": "23293827",
"level": 4
}, {
"id": "23293839",
"name": "Balance · L2",
"active": true,
"parentid": "23293807",
"level": 2
}]
}
}
let DOArr = dataConceptDetailsObj['selectedDC']['DC']
.map((item) => {
return {
id: item.id,
name: item.name
}
})
And for the second part:
Use the find
method of the DOArr array instead of includes
. Thus allowing you to check both the id and name.
> The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. -MDN
const formControls = this.dataOwnerList.map(
(control) => {
let item = this.DOArr.find(
(i) => i.id == control.id && i.name == control.name
);
return new FormControl((item !== undefined));
}
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论