zones = [
{zone:'A' : price:'20'},
{zone:'B' : price:'20'},
{zone:'C' : price:'20'},
{zone:'A' : price:'20'},
];
this is my array of objects how do I find the duplicate value if zone 'A' is Repeated twice I need to Find That?
zones = [{
zone: 'A',
price: '20'
},
{
zone: 'B',
price: '20'
},
{
zone: 'C',
price: '20'
}
];
var result = []
for (let i = 0; i < zones.length; i++) {
for (let k = 1; k < zones.length; k++) {
if (zones[i].zone === zones[k].zone && i != k && result.indexOf(zones[i].zone) == -1) {
result.push(zones[i].zone)
console.log(zones[i].zone)
}
}
}
found this answer here : post 1
var zones = [
{zone:'A' : price:'20'},
{zone:'B' : price:'20'},
{zone:'C' : price:'20'},
{zone:'A' : price:'20'},
];
unique = [...new Set(zones.map(propYoureChecking =>
propYoureChecking.zone))];
if (unique.length === 1) {
console.log(unique);
}
and for normal arrays : post 2
You can do it a few different ways: here's one (from the post):
const arry = [1, 2, 1, 3, 4, 3, 5];
const toFindDuplicates = arry => arry.filter((item, index) => arr.indexOf(item) !== index)
const duplicateElementa = tofindDuplicates(arry);
onsole.log(duplicateElements);
// Output: [1, 3]
Your requirement is a bit vague but assuming you want to find the index of the duplicate object(s).
zones = [
{zone:'A', price:'20'},
{zone:'B', price:'20'},
{zone:'C', price:'20'},
{zone:'A', price:'20'},
];
const zoneAIndices = [];
zones.forEach((zone, index) => {
if( zone.zone === 'A') {
zoneAIndices.push(index);
}
});
Now zoneAIndices[1] has the index of the duplicate item.
Note:
the zones array you have defined is incorrect, instead of : between zone and price it should be a ,