如何将逗号分隔的本地存储字符串转换为JavaScript中的数字数组 [英] How to convert comma separated string of local storage into numeric array in javascript

查看:45
本文介绍了如何将逗号分隔的本地存储字符串转换为JavaScript中的数字数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有一个一维整数数组,我想从逗号分隔的字符串中添加数据,有没有简单的方法呢?

I have a one-dimensional array of integer in JavaScript that I'd like to add data from comma separated string, Is there a simple way to do this?

例如:var strVale ="130,235,342,124";

e.g : var strVale = "130,235,342,124 ";

推荐答案

您可以拆分然后使用 parseInt 将其转换为整数数组.

You can split and then use parseInt to convert it into integer array.

//get this from localStorage
var strValue = "130,235,342,124"; 
var res = strValue.split(',').map(x=>{return parseInt(x)});
console.log(res);

这篇关于如何将逗号分隔的本地存储字符串转换为JavaScript中的数字数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆