为了账号安全,请及时绑定邮箱和手机立即绑定

重新排序对象 - 将对象复制(剪切)到另一个对象中

重新排序对象 - 将对象复制(剪切)到另一个对象中

慕森卡 2022-06-05 16:12:31
数据const data = [  {    "ID": 1,    "parent": "0",    "title": "Test-1"  },  {    "ID": 2,    "parent": "1",    "title": "Test-1-1"  },  {    "ID": 3,    "parent": "0",    "title": "Test-2"  },  {    "ID": 4,    "parent": "0",    "title": "Test-3"  },  {    "ID": 5,    "parent": "4",    "title": "Test-3-1"  },  {    "ID": 6,    "parent": "4",    "title": "Test-3-2"  },];操作:let parents = data.filter(item => item.parent == "0");let parents_children = parents.map(item => item.children = data.filter(stop => stop.parent == item.ID));console.log(parents_children);根本不工作。console.log(parents);工作正常。但我不明白为什么......let parents_children影响如何let parents?对于我要归档的内容,是否有更好的解决方案?
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

分配和返回必须不同


let parents_children = parents.map(item => {

   item.children = data.

           filter(stop => stop.parent == item.ID);

   return item;

});

// 更好的性能


let result = data2.reduce((map, cur) => {

  if (cur.parent === "0") {

    // parent

    map[cur.ID] = map[cur.ID] || { children: [] };

    map[cur.ID] = { ...map[cur.ID], ...cur };

  } else {

    if (!map[cur.parent]) map[cur.parent] = { children: [] };

    map[cur.parent].children.push(cur);

  }

  return map;

}, {});

let finalResult = [];

for (const key in result) {

  finalResult.push(result[key]);

}

console.log(JSON.stringify(finalResult, null, 4));

工作样本:


const data = [

  { ID: 1, parent: "0", title: "Test-1" },

  { ID: 2, parent: "1", title: "Test-1-1" },

  { ID: 3, parent: "0", title: "Test-2" },

  { ID: 4, parent: "0", title: "Test-3" },

  { ID: 5, parent: "4", title: "Test-3-1" },

  { ID: 6, parent: "4", title: "Test-3-2" }

];


let parents = data.filter(item => item.parent == "0");

let parents_children = parents.map(item => {

  // create new object

  return {

    ...item,

    children : data.filter(stop => stop.parent == item.ID)

  };

});

console.log(JSON.stringify(parents_children, null, 4));

console.log(data);



const data2 = [

  { ID: 1, parent: "0", title: "Test-1" },

  { ID: 2, parent: "1", title: "Test-1-1" },

  { ID: 3, parent: "0", title: "Test-2" },

  { ID: 4, parent: "0", title: "Test-3" },

  { ID: 5, parent: "4", title: "Test-3-1" },

  { ID: 6, parent: "4", title: "Test-3-2" }

];


// Better performance



let result = data2.reduce((map, cur) => {

  if (cur.parent === "0") {

    // parent

    map[cur.ID] = map[cur.ID] || { children: [] };

    map[cur.ID] = { ...map[cur.ID], ...cur };

  } else {

    if (!map[cur.parent]) map[cur.parent] = { children: [] };

    map[cur.parent].children.push(cur);

  }

  return map;

}, {});

let finalResult = [];

for (const key in result) {

  finalResult.push(result[key]);

}

console.log(JSON.stringify(finalResult, null, 4));

.as-console-row {color: blue!important}


查看完整回答
反对 回复 2022-06-05
?
繁花如伊

TA贡献2012条经验 获得超12个赞

.map()你忘了从我相信回来。


尝试以下操作:


let parents_children = parents.map(item => {

   item.children = data.filter(stop => stop.parent == item.ID)

   return item;

});

我希望这有帮助!


查看完整回答
反对 回复 2022-06-05
  • 2 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信