1

How can we remove duplicate items for the root of an array using javascript. The code below removes duplicate items that are nested in the array. I am hoping to remove the root duplicate.

let myItems = [{
    "Visible": true,
    "title": "Folder 1",
    "Children": [{
      "title": "item in folder",
      "IsGIF": false,
      "IsWeb": false
    }]
  },
  {
    "title": "item in folder",
    "IsGIF": false,
    "IsWeb": false
  }
]

let stringArray = myItems.map(JSON.stringify);
let uniqueStringArray = new Set(stringArray);
let uniqueArray = Array.from(uniqueStringArray, JSON.parse);

console.log(uniqueArray);

//outputs:


[{
    Visible: true,
    title: 'Folder 1',
    Children: [
      [Object]
    ]
  },
  {
    title: 'item in folder',
    IsGIF: false,
    IsWeb: false
  }
]

Hoping to get the following output:

[
  {
    "Visible": true,
    "title": "Folder 1",
    "Children": [
      {
        "title": "item in folder",
        "IsGIF": false,
        "IsWeb": false
      }
    ]
  }
]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jerry Seigle
  • 417
  • 4
  • 12
  • 4
    Your algorithm seems to function fine: there are no duplicates in your array so it doesn't filter out any. How should the function know it needs to look in the `"Children"` key of the first object? Is there always an object with `"Children"` on your first entry? I feel like what you are trying to accomplish isn't yet accurately described in your question. – soimon Jan 09 '21 at 19:30
  • Does this answer your question? [Remove duplicate values from JS array](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) – fdermishin Jan 10 '21 at 06:06

2 Answers2

0
let arr = [0,2,1,6,5,3,4,4,2,2]
newArr = []
arr.sort()

for(let i = 0; i < arr.length; i++){
    if(arr[i] != arr[i+1]){
        newArr.push(arr[i])
    }
}

console.log(newArr)

You can do something like this. It will create a new array without duplicates.

havardsj
  • 65
  • 7
0

you can traverse the array and track the items recursively.

const isSeen = (seen, item) => {
  return seen.some(s => s.title === item.title && s.IsWeb === item.IsWeb && s.IsGIF === item.IsGIF);
}

const traverse = (item, seen, parent) => {
  if (item.Children) {
    const obj = {};
    Object.keys(item).filter(
      k => k !== "Children"
    ).forEach(
      k => obj[k] = item[k]
    )
    obj.Children = [];
    parent.push(obj);
    item.Children.forEach(child => traverse(child, seen, obj.Children));
  } else if (!isSeen(seen, item)) {
    seen.push(item);
    parent.push(item);
  }
}


const removeDuplicates = (arr) => {
  const seen = [];
  const results = [];
  arr.forEach(item => traverse(item, seen, results));
  return results;
};


const result = removeDuplicates([{
    "Visible": true,
    "title": "Folder 1",
    "Children": [{
      "title": "item in folder",
      "IsGIF": false,
      "IsWeb": false
    }]
  },
  {
    "title": "item in folder",
    "IsGIF": false,
    "IsWeb": false
  }
]);

console.log(result);
D. Seah
  • 4,472
  • 1
  • 12
  • 20