繁体   English   中英

如何将一组数组(字符串格式)推入数组?

[英]How to push a group of arrays (string format) to an array?

我从数据库中以这种格式[52,16,135],[54,16,140],[22,16,140]获取一组数组,我需要将其所有元素分别推送到新数组中,但是看起来像我的代码添加了它们作为代码的数组

在此处输入图片说明

但是我需要的是

[
    "True",
    "52",
    "16",
    "135",
    "54",
    "16",
    "140",
    "22",
    "16",
    "140",
    "Other Value"
]

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var arr =[]; arr.push('True'); arr.push(str.replace(/\\[/g, "").replace(/\\]/g, "").split(',')); arr.push('Other Value'); console.log(arr); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

var str = '[52,16,135],[54,16,140],[22,16,140]';
var arr =[];
arr.push('True');
arr.push(str.replace(/\[/g, "").replace(/\]/g, "").split(','));
arr.push('Other Value');

只需将.flat()方法添加到结果中即可。 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/flat enter code here

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var arr =[]; arr.push('True'); arr.push(str.replace(/\\[/g, "").replace(/\\]/g, "").split(',')); arr.push('Other Value'); console.log(arr.flat()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

对于您的代码,一种简单的解决方法是使用Array.push()方法内部的传播语法来传播split()生成的数组的所有元素,作为push()方法的参数。 另外,您可以使用一个replace()句子replace(/[[\\]]/g, "")来替换您拥有的两个。

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var arr =[]; arr.push('True'); arr.push(...str.replace(/[[\\]]/g, "").split(',')); arr.push('Other Value'); console.log(arr); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

你快到了。 只是将其串联而不是推送。

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var innerArr = str.replace(/\\[/g, "").replace(/\\]/g, "").split(','); var arr = ["True"].concat(innerArr).concat(["Other Value"]); console.log(arr); 

除了使用replace ,您还可以在字符串周围添加[]以创建有效的2D数组JSON。 然后parse字符串并使用flat

 var str = '[52,16,135],[54,16,140],[22,16,140]'; const newArray = JSON.parse(`[${str}]`).flat(); console.log(newArray) 

如果浏览器不支持flat 文字模板文字,则为ES5解决方案:

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var parsed = JSON.parse('[' + str + ']'), newArray = [].concat.apply([], parsed); console.log(newArray) 

好吧,您有2个我可以想到的即时选择:

第一种选择: ES6扩展语法

 const str = '[52,16,135],[54,16,140],[22,16,140]'; const strArr = str.replace(/\\[/g, "").replace(/\\]/g, "").split(','); const arr = ['True', ...strArr, 'Other Value']; console.log(arr); 

第二个选择: Array.prototype.concat()

 const str = '[52,16,135],[54,16,140],[22,16,140]'; const strArr = str.replace(/\\[/g, "").replace(/\\]/g, "").split(','); let arr = []; arr.push('True'); arr = arr.concat(strArr); arr.push('Other Value'); console.log(arr); 

如果可以的话,我会选择选项1。

干杯:)

尝试

['True', ...str.match(/\d+/g), 'Other Value'];

 var str = '[52,16,135],[54,16,140],[22,16,140]'; var arr = ['True',...str.match(/\\d+/g),'Other Value']; console.log(arr); 

暂无
暂无

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

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