我试图用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
来修改密钥,但是我不知道如何以最佳方式迭代数组。
我们非常感谢任何建议。
答案 0 :(得分:2)
我不想改变原始数据,所以这就是我要做的事情:
const thing = [{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"}]}];
const newThing = [];
thing.map( item => {
newThing.push(
_.mapKeys( item, ( value, key ) => {
let newKey = key;
if( key === 'id' ) {
newKey = 'value';
}
if( key === 'name' ) {
newKey = 'title';
}
return newKey;
})
)
});
console.log( newThing );
答案 1 :(得分:1)
使用Array#map(或lodash的_.map()
)进行迭代,并使用Object#assign(或_.assign()
)生成带有更改密钥的新对象。
分配将使用新键合并一个对象,其中包含原始对象中的相应值,以及没有要使用_.omit()
替换的键的原始对象。
var data = [{"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"}]}];
var result = data.map(function(o) {
return Object.assign({
value: o.id,
title: o.name
}, _.omit(o, 'id', 'name'));
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
答案 2 :(得分:1)
您可以使用_.map
并创建一个新对象,根据条件更改密钥并返回新对象。
var obj = [{
"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"
}]
}];
var updatedObj = _.map(obj, function(currObj, index) {
var newObj = {};
Object.keys(currObj).forEach(function(key) {
if(key === 'id') {
newObj.value = currObj[key];
} else if(key === 'name') {
newObj.name = currObj[key];
} else {
newObj[key] = currObj[key];
}
});
return newObj;
});
console.log(updatedObj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>