I have the below array
const dummyData =[
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer B",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer C",
"shelf":"12",
"box":"11",
"position":"13",
},
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"5",
"box":"2",
"position":"3",
},
{
"plateID":"1234567",
"freezer":"fridzer C",
"shelf":"12",
"box":"11",
"position":"13",
},
]
I want to match "freezer","shelf","box","position" value and find the index of duplicate value.
The output will be like
[
0,
2,
3,
5
]
I have tried the below approach but only able to match one column at a time (Not all columns)
let duplicates = [];
let tempArray = {};
dummyData.forEach((item, index) => {
tempArray[item.freezer] = tempArray[item.freezer] || [];
tempArray[item.freezer].push(index);
});
for (var key in tempArray) {
if (tempArray[key].length > 1) {
duplicates = duplicates.concat(tempArray[key]);
}
console.log(duplicates);
console.log(tempArray);
}
Is there ay way to match all four columns at a time and find the index of duplicate value ?
CodePudding user response:
Hi You can use a find to verify if the object with the specific columns is already on the temp array or not, like this:
const dummyData = [
{
plateID: "1234567",
freezer: "fridzer A",
shelf: "1",
box: "1",
position: "1",
},
{
plateID: "1234567",
freezer: "fridzer B",
shelf: "1",
box: "1",
position: "1",
},
{
plateID: "1234567",
freezer: "fridzer C",
shelf: "12",
box: "11",
position: "13",
},
{
plateID: "1234567",
freezer: "fridzer A",
shelf: "1",
box: "1",
position: "1",
},
{
plateID: "1234567",
freezer: "fridzer A",
shelf: "5",
box: "2",
position: "3",
},
{
plateID: "1234567",
freezer: "fridzer C",
shelf: "12",
box: "11",
position: "13",
},
];
let duplicates = [];
let tempArray = [];
dummyData.forEach((element, key) => {
let isAlreadyAdded = tempArray.find(
(item) =>
item.freezer === element.freezer && item.shelf === element.shelf
);
if (!isAlreadyAdded) {
tempArray.push(element);
} else {
duplicates.push(key);
}
});
console.log(duplicates, tempArray);
CodePudding user response:
From my understanding, you'll need to compare every element with every other element in the array of objects
here's my approach,
const dummyData =[
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer B",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer C",
"shelf":"12",
"box":"11",
"position":"13",
},
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"1",
"box":"1",
"position":"1",
},
{
"plateID":"1234567",
"freezer":"fridzer A",
"shelf":"5",
"box":"2",
"position":"3",
},
{
"plateID":"1234567",
"freezer":"fridzer C",
"shelf":"12",
"box":"11",
"position":"13",
},
]
let duplicates = new Set()
for(let i = 0; i< dummyData.length; i ){
for(let j = i 1; j< dummyData.length; j ){
if(JSON.stringify(dummyData[i]) == JSON.stringify(dummyData[j])){
duplicates.add(i);
duplicates.add(j);
}
}
}
console.log(Array.from(duplicates));
Output:
[ 0, 3, 2, 5 ]