gpt4 book ai didi

javascript - 根据其他属性(语言)更改 JavaScript 属性

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

我想根据语言使用 JavaScript 更改对象 department_descr:

var data = {
"language": "EN",
"contacts": [
{
"department_descr": {
"en": "Management",
"fr": "Gestion"
}
}
]
};

输出:

{  
"language": "EN",
"contacts": [
{
"department_descr": "Management"
}
]
}

最佳答案

首先,您的示例对象格式错误,这将导致语法错误。将其更改为:

var data = {
"language": "EN",
"contacts": [
{
"department_descr": {
"en": "Management",
"fr": "Gestion"
}
}
]
}

其次,在这种情况下不建议更改对象。您应该使用您需要的模型创建一个新对象:

var desc = data.contacts[0].department_descr[data.language.toLowerCase()];
var newData = Object.assign({}, data, { contacts: [ { department_descr: desc } ] })

关于javascript - 根据其他属性(语言)更改 JavaScript 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276614/

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