删除重复的parentes和children数组嵌套

时间:2017-10-19 07:38:32

标签: javascript arrays

我有一个嵌套在JS中的对象数组,我想删除重复的父项和子项。 例如,从中:

{
"data": [
    {"label": "A",  
        "children":[
            {"label": "A1"},
            {"label": "A2"}
        ]
    },
    {"label": "B",
        "children":[
            {"label": "B1"},
            {"label": "B2"}
        ]
    },
    {"label": "A",  
        "children":[
            {"label": "A1"}
        ]
    },
    {"label": "B",
        "children":[
            {"label": "B1"}
        ]
    },
    {"label": "C" }         
]
}

将成为一个没有父母和子女的新数组:

{
"data": [
    {"label": "A",  
        "children":[
            {"label": "A1"},
            {"label": "A2"}
        ]
    },
    {"label": "B",
        "children":[
            {"label": "B1"},
            {"label": "B2"}
        ]
    },
    {"label": "C" }         
]
}

有可能吗?

感谢

1 个答案:

答案 0 :(得分:0)

是的,有可能。您可以使用reduce来呃将当前数据数组缩减为一个没有重复数据的新数组:

const noDupes = result.data.reduce((noDupesArray, currentItem) => {
  // Check if the current item already exists in noDupesArray.
  const parent = noDupesArray.find((parent) => parent.label === currentItem.label);

  if (parent) {
    // Set parent.children to an empty array if the parent has no children but the current item has.
    if (!parent.children && currentItem.children) {
      parent.children = [];
    }

    currentItem.children.forEach((currentItemChild) => {
      // Check if the current item's child exists in parent.children.
      const childExists = parent.children.some((child) => child.label === currentItemChild.label);

      if (!childExists) { // Push the child to parent.children if it doesn't exist yet.
        parent.children.push(currentItemChild);
      }
    });
  } else { // If current item doesn't exist yet, push it to noDupesArray.
    noDupesArray.push(currentItem);
  }

  return noDupesArray;
}, []);



const result = {
  "data": [{
    "label": "A",
    "children": [{
      "label": "A1"
    }, {
      "label": "A2"
    }]
  }, {
    "label": "B",
    "children": [{
      "label": "B1"
    }, {
      "label": "B2"
    }]
  }, {
    "label": "A",
    "children": [{
      "label": "A1"
    }]
  }, {
    "label": "B",
    "children": [{
      "label": "B1"
    }]
  }, {
    "label": "C"
  }]
};
const noDupes = result.data.reduce((noDupesArray, currentItem) => {
  // Check if the current item already exists in noDupesArray.
  const parent = noDupesArray.find((parent) => parent.label === currentItem.label);

  if (parent) {
    // Set parent.children to an empty array if the parent has no children but the current item has.
    if (!parent.children && currentItem.children) {
      parent.children = [];
    }

    currentItem.children.forEach((currentItemChild) => {
      // Check if the current item's child exists in parent.children.
      const childExists = parent.children.some((child) => child.label === currentItemChild.label);

      if (!childExists) { // Push the child to parent.children if it doesn't exist yet.
        parent.children.push(currentItemChild);
      }
    });
  } else { // If current item doesn't exist yet, push it to noDupesArray.
    noDupesArray.push(currentItem);
  }

  return noDupesArray;
}, []);

console.log(noDupes);