Concepts of Object — Part 2

📄 Table of Contents

Equality on objects

{ char: 'A'} !== { char: 'A'}
//true

Detecting duplicates in arrays of objects

const arr = [{char: 'A'}, {char: 'A'}];
const set = new Set(arr);const hasDuplicates = set.size < arr.length;
console.log(hasDuplicates)
//false
const obj1 = { char: 'A' };
const obj2 = { char: 'A' };const jsonObj1 = JSON.stringify(obj1);
//{"char":"A"}
const jsonObj2 = JSON.stringify(obj2);
//{"char":"A"}console.log(obj1 === obj2);
//false
console.log(jsonObj1 === jsonObj2);
//true
const arr = [{char: 'A'}, {char: 'A'}];
const jsonArr = arr.map(JSON.stringify);console.log(jsonArr);
//[
//"{"char":"A"}",
//"{"char":"A"}"
//]
const arr = [{char: 'A'}, {char: 'A'}];
const set = new Set(arr.map(JSON.stringify));const hasDuplicates = set.size < arr.length;
console.log(hasDuplicates)
//true
const arr = [
{id: 1, name: 'Joker'},
{id: 2, name: 'Bane'},
{id: 1, name: 'Joker' }
];const ids = arr.map(obj => obj.id);
console.log(ids);
//[1, 2, 1]
const set = new Set(ids);const hasDuplicates = set.size < arr.length;
console.log(hasDuplicates)
//true

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.