I would like to create a miltidimentional array as follows so
var multi-arr = [
["A,2,5"],
["B,4,4"],
["C,4,4"]
]
from string values gotten from the database using ajax.
string data gotten from db delimited by #
var string = "A,2,5# B,4,4# C,4,4";
I split the string by a '#' delimiter
arr1=string.split(/\s*\#\s*/g);
creating the array below
var arr = ["A,2,5", "B,4,4", "C,4,4"];
I want to further split the items in the above array using the ma ',' as a delimiter and create a multidimensional array
My problem is the loop only pushes in the last item in the array
for (i = 0; i < arr.length; i++) {
var arr2 = [];
arr2[i]=arr2.push(arr1[i].split(/\s*\,\s*/g));
}
console.log(arr2);
What am i doing wrong? or what can i do better?
I would like to create a miltidimentional array as follows so
var multi-arr = [
["A,2,5"],
["B,4,4"],
["C,4,4"]
]
from string values gotten from the database using ajax.
string data gotten from db delimited by #
var string = "A,2,5# B,4,4# C,4,4";
I split the string by a '#' delimiter
arr1=string.split(/\s*\#\s*/g);
creating the array below
var arr = ["A,2,5", "B,4,4", "C,4,4"];
I want to further split the items in the above array using the ma ',' as a delimiter and create a multidimensional array
My problem is the loop only pushes in the last item in the array
for (i = 0; i < arr.length; i++) {
var arr2 = [];
arr2[i]=arr2.push(arr1[i].split(/\s*\,\s*/g));
}
console.log(arr2);
What am i doing wrong? or what can i do better?
Share Improve this question asked Jan 24, 2016 at 1:28 Victor NjorogeVictor Njoroge 3532 gold badges9 silver badges22 bronze badges 1- why not rewrite it to JSON and just parse that? – Mike 'Pomax' Kamermans Commented Jan 24, 2016 at 1:33
6 Answers
Reset to default 4You could split it, but you have all the delimiters in place anyway, just rewrite your string to be JSON-conformant and run it through JSON.parse()
:
// base string
var str = "A,2,5# B,4,4# C,4,4";
// quote text data. If the array was plain number data, we don't even need this step.
var quoted = str.replace(/([a-zA-Z]+)/g,'"$1"');
// rewrite to JSON form for a nested array
var jsonStr = "[[" + quoted.replace(/#/g,'],[') + "]]";
// done, just tell the JSON parser to do what it needs to do.
var arr = JSON.parse(jsonStr);
And that's it, arr
is now the nested array [["A",2,5],["B",4,4],["C",4,4]]
.
This should work
var str = "A,2,5# B,4,4# C,4,4";
var arr = str.split(/\s*\#\s*/g);
for (var i = 0; i < arr.length; i++) {
arr[i]=arr[i].split(/\s*\,\s*/g);
}
console.log(arr)
In your solution var arr2 = [];
needs to be outside the for
loop, or it gets redefined everytime. However, we don't really need to define a separate var and simply update the original array.
The var statement should be outside the for loop and you don't need to do the assignment and the push.
var i;
var string = "A,2,5# B,4,4# C,4,4";
var arr1 = string.split("#");
var arr2 = [];
for (i = 0; i < arr1.length; i++) {
arr2[i]=arr1[i].split(",");
}
console.log(arr2);
As suggested by Mike 'Pomax' - you could return the data from the server as JSON. If you're using PHP, that can be done like so:
header('Content-Type: application/json');
echo json_encode($queryResult);
exit;
Here is a simple solution that should work on modern browsers:
var string = "A,2,5# B,4,4# C,4,4";
var multiArray = string.split(/\s*\#\s*/g).map(function(substr) {
return [substr];
});
You just need to loop over each element in the array again after your first split. I would use Array.prototype.map
as it will return you a new array without mutating the original.
I also did a check to see if the value in the inner array was a number. if it was it is parsed to an actual number.
var arr = ["A,2,5", "B,4,4", "C,4,4"];
console.log(arr);
var multidimensional = arr.map(function(item) {
// you need to split the elements in your array
return item.split(/,/).map(function(val) {
// you may as well check if it's a string or a Number
return parseIfNumber(val);
})
});
console.log(multidimensional);
// helper functioon
function parseIfNumber(val) {
return /^[0-9.-]+$/.test(val)
? Number(val)
: val;
}
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
From your current arr
variable - ["A,2,5", "B,4,4", "C,4,4"]
- you can create your multi_arr
variable like so:
var multi_arr = [];
arr.forEach(function(a) {
multi_arr.push([a]);
});
Keep in mind, this will create the array exactly as the example of what you were looking for, which was this:
var multi-arr = [
["A,2,5"],
["B,4,4"],
["C,4,4"]
]
If you want each of the items in the inner arrays to be split as well, this will work for you:
var multi_arr = [];
arr.forEach(function(a) {
multi_arr.push(a.split(','));
});