var indianTeam = [{
firstName: "KL",
lastName: "Rahul"
}, {
firstName: "Jayant",
lastName: "Yadav"
}, {
firstName: "Umesh",
lastName: "Yadav"
}];
In above array, you can find lastname in 2nd and 3rd have duplicate values "yadav" so I want to find second duplicate (i.e umesh yadav) and replace it with its first name, leaving jayant yadav as unique.
Function I used so far
services.filterPlayers = function() {
var i,j,tempArray = [];
for (i = 0; i < indianTeam.length; i++) {
tempArray.push(indianTeam[i].lastName);
}
tempArray = tempArray.filter(function(elem, pos){
if (tempArray.indexOf(elem) !== pos) {
return true;
}
});
return tempArray;
};
Scenario one: I filtered out duplicate yadav and returned unique values to temporary with == condition signs,
Scenario two: only yadav returned to temporary with !== condition
How to replace duplicate yadav with firstname and push to same position in temporary array?
Reference : codepen link