gpt4 book ai didi

javascript - react : Filter or map Object by iterating through list of objects

转载 作者:行者123 更新时间:2023-12-01 02:11:25 25 4
gpt4 key购买 nike

我有 CutomerType 对象和 Customer 对象的列表。 Customer 对象具有 cutomerType id 属性。根据客户对象上的客户类型 ID,我必须循环或映射正确的 customerType 对象并显示名称代码。

[ {
"id" : "5436d5fd-e3ea-4e09-be4a-a80967cd72e5",
"code" : "0",
"name" : "UN"
}, {
"id" : "674b76b8-f1ac-5c14-e053-ce5e1cac867d",
"code" : "1",
"name" : "NON-UN"
}, {
"id" : "674b76b8-f1ad-5c14-e053-ce5e1cac867d",
"code" : "2",
"name" : "COS-UN"
}, {
"id" : "674b76b8-f1ae-5c14-e053-ce5e1cac867d",
"code" : "NA",
"name" : NA"
} ]


Customer
{
"id" : "1",
"name": "Jhon",
"type": "5436d5fd-e3ea-4e09-be4a-a80967cd72e5",
}

最佳答案

这就是你可以做的。

const customerCodeArray = [{
"id": "5436d5fd-e3ea-4e09-be4a-a80967cd72e5",
"code": "0",
"name": "UN"
}, {
"id": "674b76b8-f1ac-5c14-e053-ce5e1cac867d",
"code": "1",
"name": "NON-UN"
}, {
"id": "674b76b8-f1ad-5c14-e053-ce5e1cac867d",
"code": "2",
"name": "COS-UN"
}, {
"id": "674b76b8-f1ae-5c14-e053-ce5e1cac867d",
"code": "NA",
"name": "NA"
}]


const customer = {
"id": "1",
"name": "Jhon",
"type": "5436d5fd-e3ea-4e09-be4a-a80967cd72e5",
};

const getCustomerCode = (type) => {
const filterList = customerCodeArray.filter((obj) => obj.id === type);
if (filterList.length > 0) {
return filterList[0];
}
}
console.log(getCustomerCode(customer.type));

关于javascript - react : Filter or map Object by iterating through list of objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49741226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com