javascript - How to avoid adding duplicate object in a list -


this model class

export class personmodel {      perscode:number;     name:string;     contactnumber:number;     remarks:string;   } 

i'm having list of objects of personmodel

   affectedpersons: personmodel[] = []; 

how can check whether object present in list. can avoid adding duplicate values.

addvisitor() {         let visitorval : personmodel ;         visitorval = this.affectedpersons.filter(x => x.name == this.visitorname && x.contactnumber== this.visitormob && x.remarks==this.visitorremark, x => console.log("x "+ json.stringify(x)))             .map(x => x)[0];          } 

this not working. how can achieve this

define concept of "equal" in form of js function. that's for.

function objectsareequal(obj1, obj2) {   return obj1.name === obj2.visitorname &&     obj1.contactnumber === obj2.visitormob &&     obj1.remarks === objs.visitorremark; } 

now can find if object in array of objects with

const visitoralreadyinarray = arr.some(obj => objectsareequal(obj, newobj)) 

if want find actual matching object, then

const visitorval = arr.find(obj => objectsareequal(obj, newobj)) 

filter can work in case, it's not best solution. logically, filter designed narrow down list of things. that's not you're trying do. you're trying find thing. that's find for. has advantage stop once finds thing looking for. in contract, filter continue running on all elements of array, after it's found first match.

however, not ideal solution. you've hard-wired names of properties of objects solution. means every time change or add property, you'll have change part of code. that's not thing. want write better version, works no matter properties are.

to that, you'll first need switch consistent naming of properties. instead of using 1 name properties of objects in array (name) , different name properties of object trying match (visitorname), use same name both (name).

now can write function compares 2 objects make sure properties same. old problem. have decide if want compare 2 objects in nested fashion, if contain subobjects. here thread on topic. if want "shallow" comparison, can like:

function compareobjects(obj1, obj2) {   const obj1keys = object.keys(obj1);   const obj2keys = object.keyw(obj2);    return obj1keys.length === obj2keys.length &&     obj1keys.every(key => obj1[key] === obj2[key]); } 

now can use find matching object in array saying

arr.find(obj => compareobjects(obj, newobj)) 

this check all properties same. if want instead check if subset of properties change, you'll have tweak in way, passing in list of properties want check.

note none of has typescript, or angular. it's pure javascript. however, using typescript, write compareobjects function in way more type-safe insisting 2 inputs both objects, , of same type:

function compareobjects<t extends object>(obj1: t, obj2: t) {   const obj1keys = object.keys(obj1);   const obj2keys = object.keyw(obj2);    return obj1keys.length === obj2keys.length &&     obj1keys.every(key => obj1[key] === obj2[key]); } 

now compiler complain if try pass in 2 incompatible objects compared.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -