Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting array elements in javascript split function

Hi i have the below array element

var array =["a.READ","b.CREATE"]

I'm trying to split the elements based on "." using javascript split method

below is my code

var array1=new Array();
var array2 = new Array();

for (var i = 0; i < array .length; i++) {
  array1.push(array [i].split("."));
}
console.log("this is the array1 finish  ----"+array1)

The out put that i'm receiving is

[["a","READ"],["b","CREATE"]]

The expected output that i want is

array1 =["a","b"]

array2=["READ","CREATE"]

I'm stuck here any solution regarding this is much helpful

like image 985
Stack s Avatar asked Sep 22 '14 05:09

Stack s


1 Answers

You need to add to array2 and use both elements from the returned array that String.prototype.split returns - i.e. 0 is the left hand side and 1 is the right hand side of the dot.

var array = ["a.READ", "b.CREATE"]
var array1 = []; // better to define using [] instead of new Array();
var array2 = [];

for (var i = 0; i < array.length; i++) {
    var split = array[i].split(".");  // just split once
    array1.push(split[0]); // before the dot
    array2.push(split[1]); // after the dot
}
console.log("array1", array1);
console.log("array2", array2);
like image 127
Adam Avatar answered Oct 06 '22 00:10

Adam