How to remove item if exist before and add current as new?


How to remove item if exist before and add current as new ?

I need when add statusdate exist before on collectfulldata then remove exist and and add new one

and if not exist add it .

meaning i dont need two statusdata with same name exist two time .

and if statusdata not exist add it .

let textdata=$[$].text; 
let statusdata:any = this.FilterBinddata.filter(s=>s.reportSource==textdata)[0].reportSource2; 
this.collectfulldata= statusdata + "=" + "'" + textdata + "'"; 
console.log("full data is " + this.initcollectdropdowslist)

current result

full data is Text5='Lifecycle',Text6='Package',Text7='Parametric'

Expected result

if add Text8=‘car’ to collectfulldata add it because not exist
if add Text6=‘OBS’ add it but remove exist as Text6=‘Package’

You are describing an Associative Array. In Javascript terms, you’d do better to add the elements to an Object rather than an array.

let mything = {};
mything[statusdata] = textdata; //if a property already exists, it will be replaced.
//or, if you really must output a string,
let out = ""
for(const x in mything) { 
   out += x + "='" + mything[x] + "'";

(Maybe angular has a fancier way of doing this to match your expected output, but i dont know angular. So i’ve written pure JS.)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.