7

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

7 Answers 7

8

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Just a suggestion use , in console.log("array1" , array1); this will show object
1

We'll start off with a generic transpose function for two-dimensional arrays:

function transpose(arr1) {               // to transpose a 2d array
    return arr1[0].map(                  // take the first sub-array and map
        function(_, i) {                 // each element into
            return arr1.map(             // an array which maps
                function(col) {          // each subarray into
                    return col[i];       // the corresponding elt value
                }
            );
        }
    );
}

Now the solution is just

transpose(                        // transpose the two-dimensional array
    array.map(                    // created by taking the array and mapping
        function(e) {             // each element "a.READ" into
            return e.split('.');  // an array created by splitting it on '.'
        }
    )
)

Comments

0

You are adding nothing to array2. Please use indexes properly , like below:

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

for (var i = 0; i < array .length; i++) {
  array1.push(array [i].split(".")[0]);
  array2.push(array [i].split(".")[1]);
}

Comments

0

you can do something like this

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

array.forEach(function(item,index,arr){
  item.split('.').forEach(function(item,index,arr){
    if(index % 2 === 0){
      arr1.push(item);
    }else{
      arr2.push(item);
    }
  });      
});

console.log(arr1);
console.log(arr2);

DEMO

Comments

0

I guess this is a bit redundant but, the split method actually returns and array. Although your code was off you were not modifying array2. Consider the following.

var array = [ "a.READ" , "b.CREATE" ]
  , array1 = []
  , array2 = []
  // cache array length
  , len = array.length;

for ( var i = 0; i < len; i++ ) {
    // the split method returns a new array
    // so we will cache the array
    // push element 0 to array1
    // push element 1 to array2
    var newArr = array[ i ].split('.');
    array1.push( newArr[ 0 ] );
    array2.push( newArr[ 1 ] );
}

console.log( 'array1: ', array1 );
console.log( 'array2: ', array2 );

Comments

-1

Use this:

for (var i = 0; i < array .length; i++) {
  var parts = array[i].split('.');
  array1.push(parts[0]);
  array2.push(parts[1]);
}

Comments

-1

You have not assigned any value to Array2. You can do as shown below.

var array1=[];
var array2 = [];

for (var i = 0; i < array .length; i++) {
    var arrayTemp=[];
    arrayTemp.push(array [i].split("."));
    array1.push(arrayTemp[0]);          
    array2.push(arrayTemp[1]);
}

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.