从数组创建多维对象,将此数组中的值指定为新对象中的键

时间:2021-06-26 18:52:23

I have an array like this (each contain more than 2 key-values pairs,I just use 2 for the question)

我有一个这样的数组(每个包含两个以上的键值对,我只使用2个问题)

[{"name":"Joe", "id":17}, {"name":"Bob", "id":17}, {"name":"Carl", "id": 35},{"name":"son", "id": 31},{"name":"smith", "id": 29},{"name":"tom", "id": 35}]

I would like to set a multidimensional object like this. for every DISTINC "id" and inside this "id" contain "id", "name" values .

我想设置一个像这样的多维对象。对于每个DISTINC“id”,在这个“id”中包含“id”,“name”值。

{
'17' : {
    {"name":"Joe",
    "id":17},
    {"name":"Bob",
    "id":17}
},
'35' : {
    {"name":"Carl", "id": 35},
    {"name":"tom", "id": 35}
},
'31':{"name":"son", "id": 31}
},
 '29':{"name":"smith", "id": 29}
}

As you see I want to assign distinct "id" as key on first object

如您所见,我想在第一个对象上指定不同的“id”作为键

I think I may misuse brackets, sorry for that, please correct me on this too.

我想我可能会误用括号,对不起,请对此纠正我。

3 个解决方案

#1


0  

Loop over your data and add it to a new object or append it, depending if it already exists.

循环遍历数据并将其添加到新对象或附加它,具体取决于它是否已存在。

var data = [{"name":"Joe", "id":17}, {"name":"Bob", "id":17}, {"name":"Carl", "id": 35},{"name":"son", "id": 31},{"name":"smith", "id": 29},{"name":"tom", "id": 35}];

var new_data = {};

for (var i=0; i<data.length; i++)
{
    if (new_data.hasOwnProperty(data[i].id))
        new_data[data[i].id].push(data[i]);
    else
        new_data[data[i].id] = [data[i]];
}
document.getElementById("new_data").textContent = JSON.stringify(new_data, null, 4);
<pre id="new_data"></pre>

#2


2  

I'd be lazy and use underscore. It has a function called groupBy which does exactly what you need:

我会懒惰并使用下划线。它有一个名为groupBy的函数,可以完全满足您的需求:

_.groupBy(arr, 'id');

JSFiddle.

#3


0  

Why not just populate a new object?

为什么不填充一个新对象?

var obj = {};

var arr = [{"name":"Joe", "id":17}, {"name":"Bob", "id":17}, {"name":"Carl", "id": 35},{"name":"son", "id": 31},{"name":"smith", "id": 29},{"name":"tom", "id": 35}];

arr.forEach(function(k) {
   obj[k.id] = k;
})

#1


0  

Loop over your data and add it to a new object or append it, depending if it already exists.

循环遍历数据并将其添加到新对象或附加它,具体取决于它是否已存在。

var data = [{"name":"Joe", "id":17}, {"name":"Bob", "id":17}, {"name":"Carl", "id": 35},{"name":"son", "id": 31},{"name":"smith", "id": 29},{"name":"tom", "id": 35}];

var new_data = {};

for (var i=0; i<data.length; i++)
{
    if (new_data.hasOwnProperty(data[i].id))
        new_data[data[i].id].push(data[i]);
    else
        new_data[data[i].id] = [data[i]];
}
document.getElementById("new_data").textContent = JSON.stringify(new_data, null, 4);
<pre id="new_data"></pre>

#2


2  

I'd be lazy and use underscore. It has a function called groupBy which does exactly what you need:

我会懒惰并使用下划线。它有一个名为groupBy的函数,可以完全满足您的需求:

_.groupBy(arr, 'id');

JSFiddle.

#3


0  

Why not just populate a new object?

为什么不填充一个新对象?

var obj = {};

var arr = [{"name":"Joe", "id":17}, {"name":"Bob", "id":17}, {"name":"Carl", "id": 35},{"name":"son", "id": 31},{"name":"smith", "id": 29},{"name":"tom", "id": 35}];

arr.forEach(function(k) {
   obj[k.id] = k;
})