0

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.

ramesh
  • 97
  • 1
  • 9
  • Isn't a bigger issue, **why** the id is duplicated from wherever you're getting the data from? Seems like an XY problem. – Taplar Aug 27 '19 at 18:01
  • Any effort so far ? please post the code you have tried ? – Code Maniac Aug 27 '19 at 18:01
  • Possible duplicate of [In Javascript, how do I check if an array has duplicate values?](https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values) – mwilson Aug 27 '19 at 18:04

3 Answers3

1
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';
 })
0

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.

Andreas
  • 430
  • 5
  • 21
0

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);
});
wrxsti
  • 3,434
  • 1
  • 18
  • 30