我有2个对象,在这个mydata对象中我必须将国家/地区代码转换为国家/地区名称。 国家名称我必须从国家/地区对象中选择它。
countries = [
{
"name": "Afghanistan",
"code": "AF",
"localizationLang": "en"
},
{
"name": "Albania",
"code": "AL",
"localizationLang": "en"
}
];
mydata = [{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "AF",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
},...
];
我希望输出对象应该是
[{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "Afghanistan",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
},...
]
我需要将国家/地区代码更改为国家/地区名称。
答案 0 :(得分:2)
简单:)
mydata.map(x=>{
x.countryCode = countries.find(c=>c.code === x.countryCode).name;
return x;
})
答案 1 :(得分:0)
试试吧
var countries = [{
"name": "Afghanistan",
"code": "AF",
"localizationLang": "en"
}, {
"name": "Albania",
"code": "AL",
"localizationLang": "en"
}];
var mydata = [{
"orgCode": "ZC27693542",
"orgName": "B - Sedex Conference Demo Purpose Only",
"siteCode": null,
"siteName": null,
"countryCode": "AF",
"state": null,
"product": [],
"productArea": [],
"lastDateAudit": null,
"saqPercentage": 0.0,
"auditCode": null
}, {
"orgCode": "ZC27695039",
"orgName": "Sonata-AB-Member",
"siteCode": "ZS27695161",
"siteName": "UEM group",
"countryCode": "AL",
"state": null,
"product": [],
"productArea": ["50203200", "50201700", "50202300"],
"lastDateAudit": null,
"saqPercentage": 13.0,
"auditCode": null
}];
var newArr = mydata.map(function(value) {
var country = countries.find(function(countrie) {
return countrie.code == value.countryCode;
});
value.countryCode = country.name;
return value;
});
console.log(newArr)

<强>演示:强> https://codepen.io/anon/pen/QmeEXe