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.

3
  • Isn't a bigger issue, why the id is duplicated from wherever you're getting the data from? Seems like an XY problem. Commented Aug 27, 2019 at 18:01
  • Any effort so far ? please post the code you have tried ? Commented Aug 27, 2019 at 18:01
  • Possible duplicate of In Javascript, how do I check if an array has duplicate values? Commented Aug 27, 2019 at 18:04

3 Answers 3

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';
 })
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);
});

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.