1

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

enter image description here

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

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Just add .flat() method to your result. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatenter 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>

Comments

1

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

Comments

1

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)

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.