I have a Javascript array.
var myarray = ["100", "200", "", ""];
I am trying to change all blank values in the array to Number.NaN.
I can find examples online to change falsey values to 0, but I am not sure how to do it the other way around?
Try using map function
const newArray=myarray.map(no=>{return parseInt(no)})
parseInt should have a radix argument. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…To change all the empty strings in your existing array in place to NaN, you can loop over the elements of the array:
var myarray = ["100", "200", "", ""];
for (let i = 0; i < myarray.length; i++){
if (myarray[i] === "") {
myarray[i] = NaN;
}
}
console.log(myarray);
To return a new array with the empty strings replaced by NaN, you can use Array.map:
var myarray = ["100", "200", "", ""];
const newArray = myarray.map(el => el === "" ? NaN : el);
console.log(newArray);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
null. I suggest you edit your title, it's misleading and incorrect. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…