JavaScript 中查找与 formcontrol 对象匹配的属性。

huangapple go评论81阅读模式
英文:

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。
我想要将那些 idname 与上面匹配的复选框表单控件设置为true/已选中。

const formControls = this.dataOwnerList.map(
      (control) => {
        if(this.DOArr.includes(control.id)){
          return new FormControl(true);
        }
        else{
          return new FormControl(false);
        }
      }
    );

现在,我只是检查 id,但我也想检查 name,基本上需要强力检查 idname 的匹配。

英文:

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,而是返回一个属性包,其中包含idname键,它们的值与当前项对应。

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,这样可以同时检查idname

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));
    }
);

huangapple
  • 本文由 发表于 2023年3月3日 23:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定