javascript - How Can I replace "NaN" with a space in an array - Stack Overflow

I need to replace "NaN" with a space in an arrayvar result = [1, 2, 3, NaN]console.log(resul

I need to replace "NaN" with a space in an array

var result = [1, 2, 3, NaN]
console.log(result)

I would like the output to be [1, 2, 3, " "]

I need to replace "NaN" with a space in an array

var result = [1, 2, 3, NaN]
console.log(result)

I would like the output to be [1, 2, 3, " "]

Share Improve this question asked Jan 31, 2019 at 3:19 Felipe the SheepyFelipe the Sheepy 455 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

You can simply do this:

result.map(value => isNaN(value) ? ' ' : value);

Use isNaN() for checking in forEach

var result = [1, 2, 3, NaN]
arr=[];
result.forEach((e)=>!isNaN(e)?arr.push(e):arr.push(''))
console.log(arr)

Using map

var result = [1, 2, 3, NaN];
console.log(result.map((e)=>isNaN(e)?"":e))

you can use isNaN() to find NaN elements.

var result = [1, 2, 3, NaN];

for (var i in result)
    if (isNaN(result[i]))
        result[i] = "";

console.log(result);

Use map():

var result = [0, 1, 2, 3, NaN]
result = result.map(e=>isNaN(e)?' ':e)
console.log(result)

You can use findIndex. See example below:

var result = [1, 2, 3, NaN];
var i = result.findIndex(Number.isNaN);
result[i] = " ";
console.log(result);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492621a4630052.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信