简体   繁体   中英

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

I am getting a group of array from database in this format [52,16,135],[54,16,140],[22,16,140] and I need to push all elements of of it into new array separately but looks like my code adding them as an array to the code

在此处输入图片说明

but what I need is

[
    "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');

Just add .flat() method to your result. https://developer.mozilla.org/en-US/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> 

An easy fix to your code will be using the spread syntax inside the Array.push() method to spread all the elements of the array generated by split() as arguments of the push() method. Also, you can use one replace() sentence replace(/[[\\]]/g, "") in replacement of the two you have.

 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;} 

You're almost there. Just instead of pushing, concatenate.

 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); 

Instead of using replace , you could add a [] around the string to create a valid JSON of 2D array. Then parse the string and use flat .

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

ES5 solution if flat and template literals aren't supported in your browser:

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

Well, you have 2 immediate options I can think of:

First option: ES6 spread syntax

 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); 

Second option: 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); 

I would go with option 1 if that's possible for you.

Cheers :)

try

['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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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