Home > front end >  Why is my function which is supposed to return an array with no dupes removing non-dupe values too?
Why is my function which is supposed to return an array with no dupes removing non-dupe values too?

Time:11-03

So I've got an array of arrays, each with a value and a name corresponding to said value. Here I hard-coded it, but in reality it's not.

I have a function which is supposed to return the array it was given, but with only ONE of each value. However, the function is removing other values too. Here is the code:

const scorersArr = [
  [2, 'Lewandowski'],
  [1, 'Gnarby'],
  [2, 'Lewandowski'],
  [1, 'Hummels'],
]

const returnNoDupes = (arr) => {
  let returnArr = arr;
  for (const arrVal1 of arr) {
    for (const arrValLoop of arr) {
      if (arrVal1 === arrValLoop) returnArr.splice(arrVal1, 1)
    }
  }
  return returnArr;
}

console.log(returnNoDupes(scorersArr)); //. Expected: [[2, "Lewandowski"], [1, "Hummels"], [1, "Gnarby"]]

I know that the bug has something to do with the fact that I have a 2D array, as this function works on a 1D array but I can't quite put my finger on what it is.

CodePudding user response:

Try this

const scorersArr = [
  [2, 'Lewandowski'],
  [1, 'Gnarby'],
  [2, 'Lewandowski'],
  [1, 'Hummels'],
]

const duplicatesRemoved = Array.from(new Set(scorersArr.map(JSON.stringify)), JSON.parse)

console.log(duplicatesRemoved)

A simplified/non-advanced JS solution

var scorersArr = [
  [2, 'Lewandowski'],
  [1, 'Gnarby'],
  [2, 'Lewandowski'],
  [1, 'Hummels'],
];

var nonDupesStringArray = [];

for (let i = 0; i < scorersArr.length - 1; i  ) {
 for (let j = 0; j < scorersArr.length; j  ) {
   let currentArrayString = JSON.stringify(scorersArr[i]);
   let currentComporisonArrayString = JSON.stringify(scorersArr[j]);

   let isADuplicate = currentArrayString !== currentComporisonArrayString;
   let isNotInDuplicatesArray = nonDupesStringArray.indexOf(currentComporisonArrayString) < 0;

   if (isADuplicate && isNotInDuplicatesArray)
    nonDupesStringArray.push(JSON.stringify(scorersArr[j]))
 }
}

// convert strings back into array
var nonDupes = []
for (let i = 0; i < nonDupesStringArray.length; i  ) {
  nonDupes.push(JSON.parse(nonDupesStringArray[i]))
}

console.log(nonDupes)

I hope this helps

CodePudding user response:

You can try like this,

  const scorersArr = [
  [2, 'Lewandowski'],
  [1, 'Gnarby'],
  [2, 'Lewandowski'],
  [1, 'Hummels'],
  ]

  const returnNoDupes = (arr) => {
  for (let i =0 ; i<arr.length ; i  ){
    for (let j =0 ; j<arr.length ; j  ) {
    if (arr[i][1] === arr[j][1] && i!==j){ 
        arr.splice(j, 1)
    }
   }}
   return arr;
 }
 console.log(returnNoDupes(scorersArr));

CodePudding user response:

Check the below snippet with Array.reduce() method

const scorersArr = [
    [2, 'Lewandowski'],
    [1, 'Gnarby'],
    [2, 'Lewandowski'],
    [1, 'Hummels'],
];

const returnNoDupes = (arr) => {
    return arr.reduce((acc, item) => {
        if (!acc[1].includes(item.join())) {
            acc[0].push([...item]);
            acc[1].push(item.join());
        }
        return acc;
    }, [[], []])[0];
};

console.log(returnNoDupes(scorersArr));

  • Related