-2

I have the following array:


arr = [
    [
        {
            id: 1,
            name: 'name 1',
            pushname: 'name 1'
        }
    ],
    [
        {
            id: 2,
            name: 'name 2',
            pushname: 'name 2'
        }
    ],
    [
        {
            id: 1,
            name: 'name 1',
            pushname: 'name 1'
        }
    ]
]

/*return:
 [
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ],
  [ { id: 2, name: 'name 2', pushname: 'name 2' } ],
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ]
]

*/

How could I remove the repeated information?

I would like to return:

/*
 [
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ],
  [ { id: 2, name: 'name 2', pushname: 'name 2' } ]
]
*/

I believe I would use the filter, but I tried it in several ways and didn't find a solution. Anyone have any idea how to help me?

Note: I'm a beginner! Sorry if the question is repeated, I'll exclude it if it is!

  • "I tried it in several ways and didn't find a solution", please add those attempts to your question so that we can with them. – zero298 Aug 07 '21 at 03:27
  • This should help: https://stackoverflow.com/questions/45439961/remove-duplicate-values-from-an-array-of-objects-in-javascript#45440277 – Ajay Gupta Aug 07 '21 at 03:31
  • @Ajay I tried with the link that sent me but it didn't work. The amount returned to me was just `[ [ { id: 1, name: 'name 1', pushname: 'name 1' } ] ]` – Matheus Nascimento Aug 07 '21 at 03:35

2 Answers2

-1

So as per your case, this can help

arr = [
  [
    {
      id: 1,
      name: "name 1",
      pushname: "name 1",
    },
  ],
  [
    {
      id: 2,
      name: "name 2",
      pushname: "name 2",
    },
  ],
  [
    {
      id: 1,
      name: "name 1",
      pushname: "name 1",
    },
  ],
];

// Create a reference Array
let visitedID = [];

// Perform filter operation
arr = arr.filter((element) => {
//   check if the value is present (repetetion)
  if (visitedID.includes(element[0].id)) {
    return false;
  } else {
    // Push the data to the array and return
    visitedID.push(element[0].id);
    return true;
  }
});

console.log(arr);

Explanation : So for this condition to work, you have to make sure the elements are unique with ID, And we basically create a Array of ID's and filter them based on the ID and whether they are available in the visitedID Array.

Also I am confused, why you are creating an Array[Array[Object]] instead, we can use Array[Objects] and the filter part will also be easy. If so you wish to change the form, you can use the answers mentioned in the comments.

EDIT : You can follow this shorthand approach from this question. This should help Remove duplicate values from an array of objects in javascript

SARAN SURYA
  • 534
  • 5
  • 14
-1

So.. first things first.

Comparing Array in JS is a bit tricky, if you want to learn more i would refer you to this question: How to compare arrays in JavaScript?

My Solution to this problem would be the following:

function removeDuplicates(source) {
 const newArray = [];   //Create a new array where the results are stored as String
 for(let element of source){ //Iterate through the provided array (source)
    const stringified = JSON.stringify(element) //Convert the current element into a JSON String
    if(!newArray.includes(stringified)) //Check if the Element is alread added into the new List, if not add it
        newArray.push(stringified) //Add it here
 }

return newArray.map(JSON.parse); //Convert the String-Array into an Object-Array and return
}

This is probably not the most elegant solution, but you got the special case that you got an array, which contains arrays which coomplicates the things.

Hope i could help :) if you got follow-up questions feel free to ask!

Justin G.
  • 11
  • 7