[英]How to convert array of key–value objects to array of objects with a single property?
I have an array of objects like this:我有一个像这样的对象数组:
[
{ "key": "fruit", "value": "apple" },
{ "key": "color", "value": "red" },
{ "key": "location", "value": "garden" }
]
I need to convert it to the following format:我需要将其转换为以下格式:
[
{ "fruit": "apple" },
{ "color": "red" },
{ "location": "garden" }
]
How can this be done using JavaScript?如何使用 JavaScript 做到这一点?
var data = [ {"key":"fruit","value":"apple"}, {"key":"color","value":"red"}, {"key":"location","value":"garden"} ]; var result = data.map(function (e) { var element = {}; element[e.key] = e.value; return element; }); console.log(result);
also if you use ES2015
you can do it like this另外如果你使用
ES2015
你可以这样做
var result = data.map((e) => {
return {[e.key]: e.value};
});
Using an arrow function, with the data called arr
使用箭头函数,数据名为
arr
arr.map(e => {
var o = {};
o[e.key] = e.value;
return o;
});
This generates a new Array and does not modify the original这会生成一个新的Array并且不会修改原来的
It can be simplified down to one line as它可以简化为一行
arr.map(e => ({[e.key]: e.value}));
If you can't assume arrow function support yet, you would write this longhand如果你还不能假设箭头函数支持,你会写这个
arr.map(function (e) {
var o = {};
o[e.key] = e.value;
return o;
});
Using map (as suggested in other answers) or the following will do what you want...使用地图(如其他答案中所建议的)或以下将做你想做的......
var data = [{"key":"fruit","value":"apple"},{"key":"color","value":"red"},{"key":"location","value":"garden"}];
var obj = {};
for(var i = 0; i < data.length; i++) {
obj[data[i]["key"]] = data[i]["value"];
}
In Javascript, obj.property and obj['property'] return same things.在 Javascript 中, obj.property 和 obj['property'] 返回相同的东西。 obj['property'] is more flexible because the key 'property' could be a string with some space :
obj['property'] 更灵活,因为键 'property' 可以是带有一些空格的字符串:
obj['pro per ty'] // work
obj.pro per ty // not work
or或者
var a = 'property';
obj.a == obj.property // => false
obj[a] == obj.property // => true
So you could try that.所以你可以试试。
var data = [{"key":"fruit","value":"apple"},{"key":"color","value":"red"},{"key":"location","value":"garden"}]
var new_data = [];
var data_length = data.length; // just a little optimisation for-loop
for (var i = 0; i < data_length; i++) {
var item = data[i]; // to have a vision close of foreach-loop (foreach item of collection)
new_data[i] = {};
new_data[i][item.key] = item.value;
}
console.log(new_data);
// [{"fruit":"apple"},{"color":"red"},{"location":"garden"}]
What you currently have is an array of object, each having two attributes, key and value.您当前拥有的是一个对象数组,每个对象都有两个属性,键和值。 If you are not aware of map, you can always run a forEach loop on this array and rearrange the data.
如果您不知道 map,您始终可以在此数组上运行 forEach 循环并重新排列数据。 Try something like below:
尝试如下:
function() {
var newArray = [];
oldArray.forEach(function(x){
var obj= {};
obj[x.key] = x.value;
newArray.push(obj);
});
console.log(newArray);
}
here oldArray is your original data这里 oldArray 是你的原始数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.