0

I have an array with 6 elements. Each elements has 3 properties: id, value, and friends.

In my code I manipulate the array by adding new values to certain elements' friends property, but in some circumstances this leads to duplicate values in the friends property. For instance, in myArray below, element 5 cannot be friends with element 1 twice -- they're either friends or not.

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": [1, 1]},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

How can I remove duplicate values in the friends property in myArray?

My desired output would look like this:

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": 1},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

I'm new to javascript and not sure where to start with this. My intuition was to first get all the friends and then apply some filtering function:

const friendList = myArray.map(getFriends);
var uniqueFriends = [...new Set(friendList)];

But this clearly doesn't work.

4
  • Your intuition was good, but the changes aren't done in-place. Commented Aug 17, 2020 at 15:02
  • 1
    There's no need to clean up that side-effect, you may use solution free of that issue Commented Aug 17, 2020 at 15:07
  • Thanks @Yevgen Gorbunkov! Just noticed the edit and gave you the answer on that post. But I'll also leave this post up as it could be helpful in future. Commented Aug 17, 2020 at 15:08
  • @user72716 You need to loop through the array and make changes to friends property as needed. See my code below. Commented Aug 17, 2020 at 15:25

4 Answers 4

2
myArray.map(elem => {
  if(typeof(elem.friends) == 'object') {
    elem.friends = [... new Set(elem.friends)]
    if(elem.friends.length == 1) {
      elem.friends = elem.friends[0]
    }
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

I think the easiest solution to this problem is to always represent the friends value with a set. So your array would look like this:

const myArray = [
    {"id": 1, "value": 75, "friends": new Set([3])},
    {"id": 2, "value": 40, "friends": new Set([4])},
    {"id": 3, "value": 60, "friends": new Set([5])},
    {"id": 4, "value": 62, "friends": new Set([6, 1])},
    {"id": 5, "value": 55, "friends": new Set([1, 1])},
    {"id": 6, "value": 33, "friends": new Set([2, 1])}
];

And then to add a friend, all you have to do is:

myArray[i]["friends"].add(7);

And duplicates are handled automatically.

Comments

1

You can write a function to perform the clean-up.

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": [1, 1]},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

const getDataWithoutDup = (data) => {
  // Loop through the array
  data.forEach(val => {

   // Check if friends property in the object is an array
   if(typeof(val.friends) == 'object') {
   let newVal = [...new Set(val.friends)];

   // Add the new value of friends
   val.friends = newVal.length > 1 ? newVal : newVal[0];
   }
  })
  console.log(data);
  return data;
}

getDataWithoutDup(myArray);

Comments

1

Try this.

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": [1, 1]},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

const friendList = myArray.map(getFriends);

function getFriends(element) {
  if(element.friends.length > 0) {
      element.friends = [... new Set(element.friends)]
      if(element.friends.length === 1) {
        element.friends =  element.friends[0];
      }
   }
   return element;
}


console.log(friendList);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.