Find Duplicate Values in Objects With JavaScript

Find duplicate values in objects with Javascript

You can use 2 reduce. The first one is to group the array. The second one is to include only the group with more than 1 elements.

var array = [{"name":"Steven Smith","Country":"England","Age":35},{"name":"Hannah Reed","Country":"Scottland","Age":23},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84}]
var result = Object.values(array.reduce((c, v) => { let k = v.name + '-' + v.Age; c[k] = c[k] || []; c[k].push(v); return c;}, {})).reduce((c, v) => v.length > 1 ? c.concat(v) : c, []);
console.log(result);

Get list of duplicate objects in an array of objects

You can use Array#reduce to make a counter lookup table based on the id key, then use Array#filter to remove any items that appeared only once in the lookup table. Time complexity is O(n).

const values = [{id: 10, name: 'someName1'}, {id: 10, name: 'someName2'}, {id: 11, name:'someName3'}, {id: 12, name: 'someName4'}];
const lookup = values.reduce((a, e) => { a[e.id] = ++a[e.id] || 0; return a;}, {});
console.log(values.filter(e => lookup[e.id]));

Find duplicate objects in array and return new array of object with number of duplicates as a new property

I think you'd be best suited by creating a helper object. A helper object will initially be empty, but will become populated slowly by what you're reading through. I'm going to assume that the keys in your array are consistent.

const keys = ["Name","Type"]
var counterObj = {}
let keyForCounterObj
arrayOfObjects.forEach((obj)=>{
keyForCounterObj = ''
keys.forEach((key)=>{
keyForCounterObj += String(obj[key])
}
if(counterObj[keyForCounterObj]){
counterObj[keyForCounterObj].times ++
}else{
counterObj[keyForCounterObj] = {
...obj,
times:1
}}}

Let's break that down, because I understand that it might be a little bit confusing if you've never seen this setup before.

We're looping through each object in the array, and we're constructing a key based on all of the values that this object is storing. For example, arrayOfObjects[0] will create a key of "AppleFruit." (I'm using the String() method just in case this is being applied to an object with only integer or floating point values, as those are invalid to create a key in javaScript. It isn't necessary for your specific question)

Once we have that key, we check to see if it exists in our counterObject. If it does not exist, then we define it. We set the "times" attribute to 1, because we just created this object; it wouldn't exist unless we had just found it.

If the object does already exist, then we just increment the "times" attribute. At the end, we have an object that looks like this:

counterObj = {
AppleFruit: {
Name:"Apple",
Type:"Fruit",
times:3,
},
CarrotVegetable:{
Name:"Carrot",
Type:"Vegetable",
times:4,
}
}

Okay, so now we have an object of objects. Let's turn that into an array!

let newArrayOfObjects = []
const counterObjKeys = Object.keys(counterObj)
counterObjKeys.forEach((key)=>{
newArrayOfObjects.push(counterObj[key])
}

This will return the final value in the format that you specified!

How to find a duplicate value in this object array?

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]

Find duplicate object based on value property in array of objects and then reduce the array concatenating the duplicate object in its label property

This could be achieved with a for loop. Loop over the objects, construct a new array, check if value already exists and if it does concatenate the label, otherwise add a new entry:

function removeDuplicates(arr) {
const newArr = [];

for (const obj of arr) {
const existing = newArr.find((el) => el.value === obj.value);
if (existing) {
existing.label += "/" + obj.label;
} else {
newArr.push(obj);
}
}

return newArr;
}

console.log(removeDuplicates([
{value: "2021", label: "REDS"},
{value: "2020", label: "REDS"},
{value: "2021", label: "COPASA"},
{value: "2021", label: "CEMIG_CLIENTES"},
{value: "2016", label: "CEMIG_CLIENTES"},
{value: "2016", label: "RFQSA"}
]));

How can I check if the array of objects have duplicate property values?

Use array.prototype.map and array.prototype.some:

var values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName4' },
{ name: 'someName2' }
];

var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});
console.log(isDuplicate);

Find duplicate value in JavaScript object

You have to write a nested loop to find that,

var keys = Object.keys(answers);
var dupe = false;

for(var i=0;i<keys.length;i++){
for(var j=i+1;j<keys.length;j++){
if(answers[keys[i]] === answers[keys[j]]){
dupe = true;
break;
}
}
if(dupe){ console.log("dupe value is there.."); break; }
}
DEMO

How to find duplicate values in a JavaScript array of objects, and output only unique values?

You could use a Set in combination with Array#map and a spread operator ... in a single line.

Map returns an array with all names, which are going into the set initializer and then all values of the set are returned in an array.

var family = [{ name: "Mike", age: 10 }, { name: "Matt", age: 13 }, { name: "Nancy", age: 15 }, { name: "Adam", age: 22 }, { name: "Jenny", age: 85 }, { name: "Nancy", age: 2 }, { name: "Carl", age: 40 }],    unique = [...new Set(family.map(a => a.name))];
console.log(unique);

JavaScript: check if duplicate key values exist in array of objects and remove all but most recently added object having that key value

You can turn it into a Map indexed by m_id, then take the map's values:

const map = new Map(
arr.map(obj => [obj.m_id, obj])
);
const deduplicatedArr = [...map.values()];

(you might be able to use an object here, but only if the respective order of non-duplicate IDs doesn't need to be preserved - since the IDs are numeric, they'll be iterated over in ascending numeric order if they were properties of an object)



Related Topics



Leave a reply



Submit