Push value into array of arrays in JavaScript -


i have , array of arrays looks this:

var arr = [[1,2,3],[4,5,6],[7,8,9]]; 

after have list of numbers , loop

var list = [15,10,11,14,13,12]  (i=0; i<list.length; i++) {       var val = list[i];      if (val >= 10 && val < 13) {      arr[arr.length].push(val);        }     else if (val >= 13 && val < 16) {      arr[arr.length+1].push(val);        } } 

so have output this:

arr = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]; 

with code i'm getting error "cannot read property 'push' of undefined"

also important can't use arr[3].push or arr[4].push because case more complicated , need push values new array appear on over of array. no matter how many objects have inside.

if looking sort array element code not work. refer below code sort element , solve undefined issue.

<script> var arr = [[1,2,3],[4,5,6],[7,8,9]]; var list = [15,10,11,14,13,12]; var arr1=[]; var arr2=[];  (i=0; i<list.length; i++) {      var val = list[i];      if (val >= 10 && val < 13) {      arr1.push(val);     }     else if (val >= 13 && val < 16) {      arr2.push(val);     }  } arr1.sort(function(a, b){return a-b}); arr.push(arr1); arr2.sort(function(a, b){return a-b}); arr.push(arr2); console.log(arr); </script> 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -