ramesh
ramesh

Reputation: 97

How to fine duplicate data in array of object using js?

I have a following array of objects like

[  
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Front",
      "tech":"Screen Print"
   },
   {  
      "id":"30771",
      "posimage":"/b/l/blue-shirt_3.jpg",
      "position":"Position Front",
      "tech":"Screen Print"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Front",
      "tech":"Embroidery"
   },
   {  
      "id":"30771",
      "posimage":"/b/l/blue-shirt_3.jpg",
      "position":"Position Front",
      "tech":"Embroidery"
   }
]

Here I have repeated values like "id":"30772", "position":"Position Front" I need to get the repeated position and need to give in the alert box like the Position Front is repeated.

Upvotes: 0

Views: 204

Answers (3)

wrxsti
wrxsti

Reputation: 3504

Did you want to find them, or remove them? Demo

var objArray = [  
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Front",
      "tech":"Screen Print"
   },
   {  
      "id":"30771",
      "posimage":"/b/l/blue-shirt_3.jpg",
      "position":"Position Front",
      "tech":"Screen Print"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Front",
      "tech":"Embroidery"
   },
   {  
      "id":"30771",
      "posimage":"/b/l/blue-shirt_3.jpg",
      "position":"Position Front",
      "tech":"Embroidery"
   }
];

function checkForDuplicates(objArr)
{
    var idArray = objArr.map(function(o){ return o.id; }),
        duplicateIds = [],
        checkedIds = [];
    $.each(idArray, function(i, id){
        if (checkedIds.indexOf(id) > -1)
        {
            duplicateIds.push(id);
        }
        else
        {
            checkedIds.push(id);
        }
    });
    return duplicateIds;
}

$(function(){
    var dupes = checkForDuplicates(objArray);
    alert(dupes);
});

Upvotes: 0

Deepak Kumar
Deepak Kumar

Reputation: 11

const items = [{  
  "id":"30772",
  "posimage":"/b/l/blue-shirt_1_1.jpg",
  "position":"Position Front",
  "tech":"Screen Print"
},
{  
  "id":"30771",
  "posimage":"/b/l/blue-shirt_3.jpg",
  "position":"Position Front",
  "tech":"Screen Print"
},
{  
  "id":"30772",
  "posimage":"/b/l/blue-shirt_1_1.jpg",
  "position":"Position Front",
  "tech":"Embroidery"
},
{  
  "id":"30771",
  "posimage":"/b/l/blue-shirt_3.jpg",
  "position":"Position Front",
  "tech":"Embroidery"
}]

items.forEach(item => {
    const result = items.filter(it => it.id === item.id)
        if (result.length > 1) {
            return 'duplicate value exists';
        } 
        return 'no duplicates';
 })

Upvotes: 1

Andreas
Andreas

Reputation: 473

Let's assume the duplicate data is stored in an array called duplicates:

const duplicates = [
    {
        "id": "30772",
        "posimage": "/b/l/blue-shirt_1_1.jpg",
        "position": "Position Front",
        "tech": "Screen Print"
    },
    {
        "id": "30771",
        "posimage": "/b/l/blue-shirt_3.jpg",
        "position": "Position Front",
        "tech": "Screen Print"
    },
    {
        "id": "30772",
        "posimage": "/b/l/blue-shirt_1_1.jpg",
        "position": "Position Front",
        "tech": "Embroidery"
    },
    {
        "id": "30771",
        "posimage": "/b/l/blue-shirt_3.jpg",
        "position": "Position Front",
        "tech": "Embroidery"
    }
];

duplicatesPositoins = [];
duplicates.forEach((value1, index1) => {
    duplicates.forEach((value2, index2) => {
        if (value1.id === value2.id && index1 !== index2) {
            if (duplicatesPositoins.length === 0) {
                duplicatesPositoins.push({index: index2, position: value2.position});
                return;
            }
            if (duplicatesPositoins[duplicatesPositoins.length - 1].index > index2) {
                return;
            }
            duplicatesPositoins.push({index: index2, position: value2.position});
        }
    });
});

console.log(duplicatesPositoins);

Without the check duplicatesPositoins[duplicatesPositoins.length - 1].index > index2, also the first occurence of the duplicated element would be pushed into the array.

Upvotes: 0

Related Questions