Blog for web designers

Remove duplicate array based on api response attribute value

Remove duplicate array based on api response attribute value is something different than a normal array. In this post we will find out how we can achieve by comparing attribute value of object array and remove the duplicate array.

Remove duplicate array simple

In the below code what we are doing is, we loop through the original array and check with the ‘if’ condition se see if our blank new array doesn’t have any of original array. If that returns true then we are going to push original array to new array which is going to add only new numbers.

Code


var originalArray = [2,6,6,9,9,8,8,8,7,4,2];
var newArray = [];
for (i=0; i<originalArray.length; i++) {  
    if (!newArray.includes(originalArray[i])) {
        newArray.push(originalArray[i]); 
    }
}
console.log(newArray); // [2,6,9,8,7,4];

Remove duplicate array based on api response attribute value

Now what if you want to check that if the attribute value is duplicate. In the below response from API you can see that the attribute_value has some duplicate number comparing to other arrays.

Code


[
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484884",
    "activity_attributes": [
      {
        "id": "868175",
        "activity_id": "484884",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484888",
    "activity_attributes": [
      {
        "id": "868182",
        "activity_id": "484888",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484975",
    "activity_attributes": [
      {
        "id": "868344",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

In order to remove duplicate array in such condition, we will perform to loop through an array. Then we will store its identifier by some function and then only return the first instance of each item.

Code

const filterByIteratee = (array, iteratee) => {

  // Empty object to store attributes as we encounter them
  const previousAttributeNames = {

  }

  return array.filter(item => {
    // Get the right value
    const itemValue = iteratee(item)

    // Check if we have already stored this item
    if (previousAttributeNames.hasOwnProperty(itemValue)) return false
    else {
      // Store the item so next time we encounter it we filter it out
      previousAttributeNames[itemValue] = true
      return true  
    }
  })
}

Now use the above function like below
code

const uniqueLinks = filterByIteratee(social_post_link, item =>
  item.activity_attributes[0].attribute_value
)

Now the above code will return my desired output.

Code


    [
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

Note: This might not be the best solution but it is very easy to understand. Reference link

Happy Learning.

Santosh Shah

My name is Santosh Shah and blogs at santosh-shah.com


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.