码迷,mamicode.com
首页 > 编程语言 > 详细

ES6 数组和字符串

时间:2018-04-02 18:10:34      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:red   color   处理   数组   nbsp   body   有一个   数组参数   ber   

从url拿到的数组参数,变成字符串。。所以得处理下。。

比如现在有一个这样的字符串:

var a = "[1,3,2]"

先转成数组:

let b = [...a]
console.log(b) //["[", "1", ",", "3", ",", "2", "]"]

再过滤:

let c = b.reduce((arr, v)=>{
          (typeof Number(v)) === ‘number‘ && !isNaN(Number(v)) && arr.push(Number(v))
          return arr
        },[])
        console.log(c) // [1, 3, 2]

 

ES6 数组和字符串

标签:red   color   处理   数组   nbsp   body   有一个   数组参数   ber   

原文地址:https://www.cnblogs.com/dodocie/p/8695515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!