【发布时间】:2019-07-11 12:51:45
【问题描述】:
我想要实现的是将对象数组转换为对象。
我的代码
var arr = data.questions
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)
结果
data
(2) [{…}, {…}]
0: {id: 48, questionaire: 94, question: "Helloworld", input_answer: null, order: 0, …}
1: {id: 49, questionaire: 94, question: "sadasdas", input_answer: null, order: 1, …}
length: 2
__proto__: Array(0)
想要实现
questions {id: 11, questionaire: 16, question: "what?", input_answer: null, order: 0, …}
【问题讨论】:
-
给出真实代码而不是从控制台复制
-
期望每个对象都有相同的键集。在这种情况下,您无法将其转换为
object,因为对象不能有重复键 -
您的情况有什么独特之处?因为对象应该有唯一的键
-
什么是
data.questions?
标签: javascript arrays angularjs angular object