check if array of objects has same values javascript code example
Example 1: array of objects how to check if property has duplicate
var yourArray = [
{Index: 1, Name: "Farley, Charley", EmployeeCode: "12", PaymentType: "Void", CheckDate: "01/04/2012"},
{Index: 2, Name: "Farley, Charley", EmployeeCode: "12", PaymentType: "Void", CheckDate: "01/04/2012"},
{Index: 3, Name: "Tarley, Charley", EmployeeCode: "12", PaymentType: "Void", CheckDate: "01/04/2012"}
];
unique = [...new Set(yourArray.map(propYoureChecking => propYoureChecking.Name))];
if (unique.length === 1) {
console.log(unique);
}
Example 2: angular list contains property
vendors.filter(function(vendor){ return vendor.Name === "Magenic" })
Example 3: javascript array contains object
if (array.index(object) !== -1) {
console.log(`my object is in my array`)
}
if(array.includes(oject)) {
console.log(`my object is in my array`)
}
Example 4: js check if array of objects contains duplicates
const arrayOfObjCopy = [...arrayOfObj];
Object.keys(arrayOfObjCopy).forEach((key) => {
arrayOfObjCopy[key] = JSON.stringify(arrayOfObjCopy[key]);
});
const arrayOfObjhasDuplicates = uniq(arrayOfObjCopy).length != arrayOfObjCopy.length;