如何将对象数组转换为对象对象

时间:2022-01-13 13:17:41

I have seen lots of answers in the other way, but i couldnt find array of objects into object of objects

我在其他方面看到了很多答案,但我无法找到对象数组到对象的对象中

the following example of what I want: i want to transform this:

以下我想要的例子:我想改变这个:

var = [
{"a" : {"min" : 0.5}},
{"b" : {"max" : 0.6}},
{"c" : {"min" : 0.3}}]

to this:

var = {
"a" : {"min" : 0.5},
"b" : {"max" : 0.6},
"c" : {"min" : 0.3}
}

array of objets to object of objects,

对象对象的对象数组,

So I can use this solver: https://github.com/JWally/jsLPSolver

所以我可以使用这个求解器:https://github.com/JWally/jsLPSolver

thanks

4 个解决方案

#1


2  

You can do this using Object.assign() and ES6 spread syntax.

您可以使用Object.assign()和ES6扩展语法来完成此操作。

var arr = [{"a" : {"min" : 0.5}}, {"b" : {"max" : 0.6}}, {"c" : {"min" : 0.3}}]

var result = Object.assign({}, ...arr);
console.log(result)

#2


1  

const obj = arr.reduce((result, item) => {
  const key = Object.keys(item)[0];
  result[key] = item[key];
  return result; 
}, {});

Array.prototype.reduce() and Object.keys() are your friends in here.

Array.prototype.reduce()和Object.keys()是你的朋友。

I think it is kind of difficult to generalize this and make it more generic, since you have to know which key of the item should be used as key of the result. But in this case, there is only one, so easy to use.

我认为很难概括这一点并使其更通用,因为您必须知道该项的哪个键应该用作结果的关键字。但在这种情况下,只有一个,这么容易使用。

#3


0  

I suppose you should create class or struct of objects.

我想你应该创建对象的类或结构。

#4


0  

You can do like this. First loop over the array and just add the key & value from each object to the new object

你可以这样做。首先在数组上循环,然后将每个对象的键和值添加到新对象

var firstArray = [{
    "a": {
      "min": 0.5
    }
  },
  {
    "b": {
      "max": 0.6
    }
  },
  {
    "c": {
      "min": 0.3
    }
  }
]

var arrayKey = {};
firstArray.forEach(function(item) {
  for (var keys in item) {
    arrayKey[keys] = item[keys]
  }


})
console.log(arrayKey)

#1


2  

You can do this using Object.assign() and ES6 spread syntax.

您可以使用Object.assign()和ES6扩展语法来完成此操作。

var arr = [{"a" : {"min" : 0.5}}, {"b" : {"max" : 0.6}}, {"c" : {"min" : 0.3}}]

var result = Object.assign({}, ...arr);
console.log(result)

#2


1  

const obj = arr.reduce((result, item) => {
  const key = Object.keys(item)[0];
  result[key] = item[key];
  return result; 
}, {});

Array.prototype.reduce() and Object.keys() are your friends in here.

Array.prototype.reduce()和Object.keys()是你的朋友。

I think it is kind of difficult to generalize this and make it more generic, since you have to know which key of the item should be used as key of the result. But in this case, there is only one, so easy to use.

我认为很难概括这一点并使其更通用,因为您必须知道该项的哪个键应该用作结果的关键字。但在这种情况下,只有一个,这么容易使用。

#3


0  

I suppose you should create class or struct of objects.

我想你应该创建对象的类或结构。

#4


0  

You can do like this. First loop over the array and just add the key & value from each object to the new object

你可以这样做。首先在数组上循环,然后将每个对象的键和值添加到新对象

var firstArray = [{
    "a": {
      "min": 0.5
    }
  },
  {
    "b": {
      "max": 0.6
    }
  },
  {
    "c": {
      "min": 0.3
    }
  }
]

var arrayKey = {};
firstArray.forEach(function(item) {
  for (var keys in item) {
    arrayKey[keys] = item[keys]
  }


})
console.log(arrayKey)