简体   繁体   English

如何从数组而不是字符串来console.log数字

[英]How to console.log numbers from array instead of strings

Heres my code 这是我的代码

var ns =['Kyle', 'Mods',1,2,3]; console.log(ns); So i dont want the strings be logged i only want the numbers from the array how? 所以我不想记录字符串,我只想要数组中的数字?

 var ns =['Kyle', 'Mods',1,2,3]; ns.forEach((element) => { if(!isNaN(element)) { console.log(element) } }) 

Checkfor each element is number or not 检查每个元素是否为数字

Try as follows 尝试如下

  var ns =['Kyle', 'Mods',1,2,3]; ns.forEach(function(ele){ if (Number.isInteger(ele)) console.log(ele); }) 

try this 尝试这个

var ns =['Kyle', 'Mods',1,2,3];

console.log(ns.filter(x=> !isNaN(x) ));

ES6 Destructuring Assignment ES6解构分配

var ns =['Kyle', 'Mods',1,2,3];
var [ , , ...numbers] = ns;
console.log(numbers);

And the ... is Spread Syntax . ...就是价差语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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