简体   繁体   English

将值推入JavaScript中的数组数组

[英]Push value into array of arrays in JavaScript

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" 通过此代码,我得到一个错误“无法读取未定义的属性'push'”

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. 同样重要的是我不能使用arr [3] .push或arr [4] .push,因为我的情况更加复杂,并且总是需要将值推送到新数组,该新数组将出现在数组的上方。 No matter how many objects I have inside. 不管我里面有多少物体。

This is happening because arr[arr.length] will always be undefined . 发生这种情况是因为arr[arr.length]将始终是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." “同样重要的是,我不能使用arr [3] .push或arr [4] .push,因为我的情况更加复杂,并且总是需要将值推送到新数组,该数组将出现在我的数组上方。我里面有几个物体。”

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]]

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[arr.length]永远不会返回任何有意义的值,请考虑一下:如果您有一个长度为6的数组,则可以使用索引0..5。 arr[6] will always return undefined because there's nothing there. arr [6]总是返回undefined,因为那里什么也没有。

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]);   
}

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>

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 ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM