简体   繁体   中英

Is there any way to add element if that not exist in an array.Add element with comma separated?

Here in ( data.class_coachee_name ) I am getting list of similar names and different names also.

I am trying to add unique name in the array with comma separate. But push method not adding in array with comma separate:

var arrCoachee = [];

for (var x = 0; x < data.class_coachee_name.length; x++) {

var coacheeoutput = data.class_coachee_name[x].coachee_id__coachee_name ;

   if(!arrCoachee.includes(coacheeoutput)){
         arrCoachee.push(coacheeoutput);
      }
}

An array is a list of elements, so comma-separated elements doesn't make sense.

You can use join() method to generate a string that includes all elements separated with commas.

Like this:

 const myUniqueArray = ['Jim', 'Craig', 'Elon', 'David']; console.log(myUniqueArray.join()); // comma-separated console.log(myUniqueArray.join(', ')); // separated with custom delimiter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM