Implode array with null keyword

I have following array like below

[1,null,null,2]

I want to implode make it as string like below

1,null,null,2

I tried

temparr.join(',')

But I am getting

1,,,2

How do i preserve the keyword null.

4 thoughts on “Implode array with null keyword”

  1. You could convert the values to string in advance, because undefined or null values are empty string by converting with join.

    If an element is undefined, null or an empty array [], it is converted to an empty string.

    const array = [1, null, null, 2];
    
    console.log(array.map(String).join(','));
    Reply
  2. You can’t make join do that, so you would have to change the input given to join in the first place. For example:

    const result = [1, null, null, 2].map(value => value === null ? "null" : value).join(",");
    console.log(result);
    Reply

Leave a Comment