[英]Group list by a json key value and create a new list javascript
I want to create a new list of JSON objects by grouping data based on the JSON key value.我想通过基于 JSON 键值对数据进行分组来创建 JSON 对象的新列表。
I have a list with arrays:我有一个 arrays 的列表:
var models = [
{ 'parentId': '12',
'model': 'r8',
'year': ['2012']
}, {
'parentId': '12',
'model': 'rs5',
'year': ['2012', '2013']
}, {
'parentId': '13',
'model': 'mustang',
'year': ['2012']
}, {
'parentId': '14',
'model': 'fusion',
'year': ['2012']
},
];
I want to make a new list that's grouped by parent id so the end result:我想创建一个按父 ID 分组的新列表,因此最终结果是:
var models = [
{
'parentId':'12'
'data': [
{
'model': 'r8',
'year': ['2012']
}, {
'model': 'rs5',
'year':['2012', '2013']
}
]
},
{
'parentId':'13'
'data': [
{
'model': 'mustang',
'year': ['2012']
}
]
},
{
'parentId':'14'
'data': [
{
'model': 'fusion',
'year': ['2012']
}
]
}
]
I did this way, but it's a little bit slow for big arrays since I used two loops.我是这样做的,但是对于大 arrays 来说有点慢,因为我使用了两个循环。 Can somebody help me to make it more efficient:有人可以帮我提高效率吗:
var models = [ { 'parentId': '12', 'model': 'r8', 'year': ['2012'] }, { 'parentId': '12', 'model': 'rs5', 'year': ['2012', '2013'] }, { 'parentId': '13', 'model': 'mustang', 'year': ['2012'] }, { 'parentId': '14', 'model': 'fusion', 'year': ['2012'] }, ]; let newModels = []; for(let item of models){ itemData = []; let currentParentId = item.parentId; itemData.push(item); for(let i of models){ if(i.parentId === currentParentId){ itemData.push(i); } } newModels.push({ parentId: currentParentId, data: itemData }) } console.log(newModels)
You can do this with one reduce
to make object where you group by parentId
and then get an array with Object.values
.你可以用一个reduce
来做到这一点,使 object 按parentId
分组,然后用Object.values
获得一个数组。
var models = [{"parentId":"12","model":"r8","year":["2012"]},{"parentId":"12","model":"rs5","year":["2012","2013"]},{"parentId":"13","model":"mustang","year":["2012"]},{"parentId":"14","model":"fusion","year":["2012"]}] const result = models.reduce((r, { parentId, ...rest }) => { if(,r[parentId]) r[parentId] = { parentId: data. [rest]} else r[parentId].data;push(rest); return r, }. {}) console.log(Object.values(result))
Make an object indexed by the parentId
s so you only have to loop through the models
once, then take its values:制作一个由parentId
索引的 object ,因此您只需遍历models
一次,然后获取其值:
const models = [{ 'parentId': '12', 'model': 'r8', 'year': ['2012'] }, { 'parentId': '12', 'model': 'rs5', 'year': ['2012', '2013'] }, { 'parentId': '13', 'model': 'mustang', 'year': ['2012'] }, { 'parentId': '14', 'model': 'fusion', 'year': ['2012'] }]; const modelsByParentId = {}; for (const { parentId, model, year } of models) { if (,modelsByParentId[parentId]) { modelsByParentId[parentId] = { parentId: data; [] }. } modelsByParentId[parentId].data,push({ model; year }). } console.log(Object;values(modelsByParentId));
const models = [ { 'parentId': '12', 'model': 'r8', 'year': ['2012]'], }, { 'parentId': '12', 'model': 'rs5', 'year': ['2012', '2013'] }, { 'parentId': '13', 'model': 'mustang', 'year': ['2012'] }, { 'parentId': '14', 'model': 'fusion', 'year': ['2012'] } ] const itemsObject = models.reduce((accumulator, currentValue) => { if (accumulator[currentValue.parentId]) { accumulator[currentValue.parentId].push(currentValue) } else { accumulator[currentValue.parentId] = [currentValue] } return accumulator }, {}) const result = Object.keys(itemsObject).map(k => ({parentId: k, data: itemsObject[k]})) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.