1

Given the following string:

var str = "[,,,1,2,,,3,4,,,,,,5,6]";

I want to replace all "empty" values with nulls. In other words, I want this result:

"[null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]"

This almost works, but it misses the first empty value:

var str = "[,,,1,2,,,3,4,,,,,,5,6]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]

Likewise, I noticed if I have trailing empty values, it misses the last one there too:

var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,]

Any ideas how I can make sure that first and last empty values are also replaced?

Thanks!

2
  • Remove the [ ] and test Commented Feb 13, 2018 at 3:13
  • everything is working as expected you have indeed removed all empty values if you just use str.split(',').map(function(x) { return x ? x : 'null' }) Commented Feb 13, 2018 at 3:14

6 Answers 6

1

As mentioned - the [ and ] are altering your return - this removes them and using the same function as your existing function - but on the shortened string and returns the correct value.

var str = "[,,,1,2,,,3,4,,,,,,5,6]";
let newStr = str.substring(1,str.length-1).split(',').map(function(x) { return x ? x : 'null' }).join(',')

console.log(newStr);
// Gives [null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]


var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";

newStr = str.substring(1,str.length-1).split(',').map(function(x) { return x ? x : 'null' }).join(',');
console.log(newStr);
// Gives null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,null

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, that makes sense. Doh! Thank you!
1

The secret is that your brackets are getting combined with your commas / numbers at the start and end of the string when it is split. In order to resolve this, you need to remove the brackets before splitting.

This can be done with .replace() on /[\][]/g:

var str = "[,,,1,2,,,3,4,,,,,,5,6,]";
console.log(
  str.replace(/[\][]/g, '').split(',').map(function(x) {
    return x ? x : 'null'
  }).join(',')
);

Comments

1

var str = "[,,,1,2,,,3,4,,,,,,5,6]";
var a = `[${str.slice(1, -1).split(",").map(n => n || "null")}]`
  
console.log(a);

Comments

1

this is going to work:

	 var str = "[,,,1,2,,,3,4,,,,,,5,6]";
	 var result = '[' + str.replace(/^\[|\]$/g, '').split(/[,]/).map(function(x) { return x ? x : 'null' }).join(',') + ']';
	 console.log(result);

Comments

0

You have removed all the empty values. "[" and "]" are not nothing.

Comments

0

String.prototype.repEmptyWithNull = 
function() {return this
  .replace(/,,/g, ',null,')
  .replace(/,,/g, ',null,')
  .replace('[,', '[null,')
  .replace(',]', ',null]')
}

console.log ("[,,,1,2,,,3,4,,,,,,5,6,]".repEmptyWithNull())
console.log ("[,,1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())
console.log ("[,1,2,,,3,4,,,,,,5,6,,]".repEmptyWithNull())
console.log ("[,1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())
console.log ("[1,2,,,3,4,,,,,,5,6,]".repEmptyWithNull())
console.log ("[1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.