1

I have and array of arrays which looks like this:

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

After that I have a list of numbers and a loop

var list = [15,10,11,14,13,12]

for (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 basically I like to have an output which will look like this:

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

With this code I'm getting an error "Cannot read property 'push' of undefined"

Also important is I can't use arr[3].push or arr[4].push because my case is more complicated and always I need to push values to new array which will appear on the over of my array. No matter how many objects I have inside.

2
  • Try explaining the use case for this code. I guarantee there's a better way to solve it. Commented Jun 13, 2015 at 17:21
  • You have to only use arr.push(val); It will append value to array. Commented Jun 13, 2015 at 17:28

4 Answers 4

4

This is happening because arr[arr.length] will always be undefined.

So you're basically doing

undefined.push(x);
// Cannot read property 'push' of undefined

"Also important is I can't use arr[3].push or arr[4].push because my case is more complicated and always I need to push values to new array which will appear on the over of my array. No matter how many objects I have inside."

This algorithm is a code smell tho and we could probably help you better if you post your actual code.

To see what I'm talking about, consider the following code

// your numbers in a random order
var xs = [7,10,2,15,4,9,14,1,8,12,5,11,3,6,13];

// sort them
xs.sort(function(a, b) { return a-b; });

// define a function that "chunks" a list into smaller parts
function chunk(xs, n) {
  function iter(ys, y, xs) {
    if (y.length === 0) return ys;
    return next(ys.concat([y]), xs);
  }
  function next(ys, xs) {
    return iter(ys, xs.slice(0,n), xs.slice(n));
  }
  return next([], xs);
}

// call our function on your sorted list
var ys = chunk(xs, 3);

console.log(JSON.stringify(ys));
//=> [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
Sign up to request clarification or add additional context in comments.

1 Comment

Naming variables in calculus taste. Awesome.
2

arr[arr.length] can never return any meaningful, think about it: if you have an array of length 6, then you have indexes 0..5 to work with. arr[6] will always return undefined because there's nothing there.

You probably need something like this:

if (val >= 10 && val < 13) {
 arr[arr.length - 1].push(val);   
}
else if (val >= 13 && val < 16) {
 arr[arr.length].push([val]);   
}

Comments

0

If you are looking sort the array element then your code will not work. Refer below code to sort the element and it will also solve your 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=[];

for (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

0

You need something like this:

var arr = [[1,2,3],[4,5,6],[7,8,9]];
var list = [15,10,11,14,13,12];
    
for (var i=0; i<list.length; i++) {
    var val = list[i];
    var index = Math.floor((val-1)/3);
    if ( arr[index] === undefined )
        arr[index] = [];
    arr[index].push(val);
    arr[index].sort();
}
    
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.