我将尝试使用lodash修改一个对象数组,但有些困难。我想要改变下面数组中对象的键名。
以下是源数组:
我希望这能有以下结果:
请注意,前两个键已被重命名。我知道可以使用
非常感谢任何建议。
以下是源数组:
[
{
"id": "AD",
"name": "Andorra",
"currency": "EUR",
"timezone": "Europe/Andorra",
"links": [
{
"rel": "self",
"href": "http://localhost:8000/api/countries/AD"
},
{
"rel": "country.currency",
"href": "http://localhost:8000/api/currencies/EUR"
}
]
},
{
"id": "AE",
"name": "United Arab Emirates",
"currency": "AED",
"timezone": "Asia/Dubai",
"links": [
{
"rel": "self",
"href": "http://localhost:8000/api/countries/AE"
},
{
"rel": "country.currency",
"href": "http://localhost:8000/api/currencies/AED"
}
]
},
...
]
我希望这能有以下结果:
[
{
"value": "AD",
"title": "Andorra",
"currency": "EUR",
"timezone": "Europe/Andorra",
"links": [
{
"rel": "self",
"href": "http://localhost:8000/api/countries/AD"
},
{
"rel": "country.currency",
"href": "http://localhost:8000/api/currencies/EUR"
}
]
},
{
"value": "AE",
"title": "United Arab Emirates",
"currency": "AED",
"timezone": "Asia/Dubai",
"links": [
{
"rel": "self",
"href": "http://localhost:8000/api/countries/AE"
},
{
"rel": "country.currency",
"href": "http://localhost:8000/api/currencies/AED"
}
]
},
...
]
请注意,前两个键已被重命名。我知道可以使用
_.mapKeys
修改键,但是我不确定如何以最好的方式迭代数组来完成此操作。非常感谢任何建议。
map
进行迭代,并在每次迭代中运行mapKeys
? - Dominik