简体   繁体   English

如何将键值对象数组转换为具有单个属性的对象数组?

[英]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 做到这一点?

You can use .map您可以使用.map

 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};
});

Example

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.

相关问题 将单属性JavaScript对象数组转换为键/值对数组 - Convert array of single-property JavaScript objects to array of key/value pairs 如何将对象数组转换为键值对 - How to convert an array of objects into key value pairs 如何将单个键值对对象转换为具有键值对的对象数组? - How to convert a single key value pair object to array of objects with key value pair? 将值数组转换为具有该值作为属性的对象数组 - Convert array of values into array of objects with that value as a property 如何将对象数组转换为单个对象 - How to convert an Array of Objects into a single Objects 使用与值相同的键将数组转换为对象数组 - Convert array to array of objects with key as same as value 如何使用 .reduce 函数将键值数组数组转换为对象数组? - How to convert an array of key–value arrays to an array of objects with .reduce function? 将数组对象键值对属性转换为单个键值对,然后将其再次插入数组 - Convert Array Objects key value pairs properties to a single key value pair and insert it into array again 如何将具有包含值、值的对象的数组转换为只有键、值的单个对象? - How to convert an array with objects that contain value,values to a single object with only key,values? 按具有日期值的单个键对对象数组进行排序 - Sort array of objects by single key with date value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM