Javascript-改进对象集合的排序
我得到一个对象数组。为了便于解释,我减少了属性,只留下了相关的属性。 我需要对这些对象进行排序,并创建一个新数组。 原始数组当然是未排序的 数组中的对象可以有任意数量的子对象。孩子们也可以有自己的孩子。但最后它们都是主对象的子对象(parent_id:null)。 //未分类Javascript-改进对象集合的排序,javascript,sorting,Javascript,Sorting,我得到一个对象数组。为了便于解释,我减少了属性,只留下了相关的属性。 我需要对这些对象进行排序,并创建一个新数组。 原始数组当然是未排序的 数组中的对象可以有任意数量的子对象。孩子们也可以有自己的孩子。但最后它们都是主对象的子对象(parent_id:null)。 //未分类 let data = [ { id: 1, name:'ParentAlpha', parent_id: null }, { id: 2, name:'ParentBeta', parent_id: null
let data = [
{
id: 1,
name:'ParentAlpha',
parent_id: null
},
{
id: 2,
name:'ParentBeta',
parent_id: null
},
{
id: 100,
name:'ChildOneAlpha',
parent_id: 1
},
{
id: 101,
name:'ChildTwoAlpha',
parent_id: 1
},
{
id: 102,
name:'SubChildOneAlpha',
parent_id: 100
},
{
id: 103,
name:'SubChildTwoAlpha',
parent_id: 100
},
{
id: 200,
name:'ChildOneBeta',
parent_id: 2
},
{
id: 201,
name:'ChildTwoBeta',
parent_id: 2
}]
我想要实现的是这个(相同的结构,但我有ommited JSON结构)
我做了一个工作,但我认为可以做得更好,我想学习更多。不管我怎么看代码,我都不知道如何改进它。如果有人能指点一些指南,我将不胜感激
我的工作代码:
let processedProducts = [],
processedid = [],
rearangedProducts = [];
let subParentProducts = data.slice();
let masterParentProducts = data.filter((product)=>{
return product.parent_id === null
});
masterParentProducts.forEach((productParent)=>{
if(processedid.indexOf(productParent.id) === -1){
processedid.push(productParent.id);
processedProducts.push(productParent);
}
data.forEach((product, index)=>{
if (product.parent_id === productParent.id){
processedProducts.push(product);
subParentProducts[index] = null;
}
if (product.id === productParent.id) {
subParentProducts[index] = null
}
})
});
subParentProducts = subParentProducts.filter(function(n){ return n !== null });
processedProducts.forEach((prod)=>{
rearangedProducts.push(prod);
let res = subParentProducts.filter((o)=> {
return o.parent_id === prod.id;
});
if (res.length > 0){
rearangedProducts.push(res[0]);
}
});
console.log(rearangedProducts)
可以使用递归创建函数来构建新的排序数组
let data=[{“id”:1,“name”:“ParentAlpha”,“parent_id”:null},{“id”:2,“name”:“ParentBeta”,“parent_id”:null},{“id”:100,“name”:“ChildOneAlpha”,“parent_id”:1},“parent_id”:1},{“id”:102,“name”:“subchildonephaeal”,“parent_id”:100},{“id”:103,“name”:“subchildtonealpha”,“parent_id”:100},{“id”:200,“name”:,“家长id:2},{“id:201”,姓名:“ChildTwoBeta”,“家长id:2}]
函数sortArray(数据,父级){
返回数据.reduce(函数(r,e){
如果(e.parent_id==parent){
r、 推(e)
var children=sortArray(数据,e.id)
if(children.length)r=r.concat(children)
}
返回r;
}, [])
}
console.log(sortArray(data,null))
请将您的努力作为.Links rot.Done包含在问题本身中。谢谢您的提示。
let processedProducts = [],
processedid = [],
rearangedProducts = [];
let subParentProducts = data.slice();
let masterParentProducts = data.filter((product)=>{
return product.parent_id === null
});
masterParentProducts.forEach((productParent)=>{
if(processedid.indexOf(productParent.id) === -1){
processedid.push(productParent.id);
processedProducts.push(productParent);
}
data.forEach((product, index)=>{
if (product.parent_id === productParent.id){
processedProducts.push(product);
subParentProducts[index] = null;
}
if (product.id === productParent.id) {
subParentProducts[index] = null
}
})
});
subParentProducts = subParentProducts.filter(function(n){ return n !== null });
processedProducts.forEach((prod)=>{
rearangedProducts.push(prod);
let res = subParentProducts.filter((o)=> {
return o.parent_id === prod.id;
});
if (res.length > 0){
rearangedProducts.push(res[0]);
}
});
console.log(rearangedProducts)