简体   繁体   English

如何简洁地创建 JavaScript 数组数组,每个数组的长度不同(每行都有不同的指定长度)?

[英]How can I tersely create JavaScript array of arrays, with different lengths for each array (each row has a different, specified length)?

I need to create a Javascript array to represent the layout of tables in a room:我需要创建一个 Javascript 数组来表示房间中表格的布局:

0 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 
17 18 19

I have done this so far:到目前为止,我已经这样做了:

this.CHART = Array(4).fill(0).map((x, i) => Array(5).fill(0).map((y, j) => j));

...which produces this array: ...产生这个数组:

0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4

This is very close to the requirement, but now I'm in over my head trying to assign the correct values, and assigning a different size to each array.这非常接近要求,但现在我无法分配正确的值,并为每个数组分配不同的大小。

This smelly version works to create the required array, but I think there must be a better way, that will be helpful to me and to others reading this post in the future:这个臭版本可以创建所需的数组,但我认为必须有更好的方法,这对我和将来阅读这篇文章的其他人会有所帮助:

ROW_1 = [];
ROW_2 = [];
ROW_3 = [];
ROW_4 = [];

const ROW_1_SIZE = 5;
const ROW_2_SIZE = 7;
const ROW_3_SIZE = 5;
const ROW_4_SIZE = 3;

ROW_1 = Array(ROW_1_SIZE).fill(0).map((x, i) => i + 1);
ROW_2 = Array(ROW_2_SIZE).fill(0).map((x, i) => i + 1 + ROW_1_SIZE );
ROW_3 = Array(ROW_3_SIZE).fill(0).map((x, i) => i + 1 + ROW_1_SIZE + ROW_2_SIZE );
ROW_4 = Array(ROW_4_SIZE).fill(0).map((x, i) => i + 1 + ROW_1_SIZE + ROW_2_SIZE + ROW_3_SIZE );

You could keep a counter for the values in a closure.您可以为闭包中的值保留一个计数器。

 var lengths = [5, 7, 5, 3], result = lengths.map((i => length => Array.from({ length }, _ => i++))(0)); result.forEach(a => console.log(...a));

Without IIFE没有 IIFE

mapWithValue is a function which expects a value and returns a callback for Array#map for creating an array and maps increased values. mapWithValue是一个函数,它需要一个值并返回Array#map的回调,用于创建数组并映射增加的值。

 const mapWithValue = i => length => Array.from({ length }, _ => i++); var lengths = [5, 7, 5, 3], result = lengths.map(mapWithValue(0)); result.forEach(a => console.log(...a));

暂无
暂无

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

相关问题 如何为数组/ JavaScript中的每个项目创建不同的按钮 - How do I create different button for each item in an array / JavaScript 如何编写一个以两次调用,以不同长度的数组作为参数,可以正确返回预期的输出数组? - How can I write a function to be called twice, with arrays of different lengths as arguments, that correctly returns expected output array? 将JavaScript数组聚合为元素长度不同的几个数组? - Aggregating JavaScript array into several arrays with different element lengths? 如何使用JavaScript将字段添加到数组的每一行? - How can I add a field to each row of an array with Javascript? 如何编写RegEx为每个图像创建一个javascript数组? - How can I write RegEx to create a javascript array of each image? 如何从两个不同长度的数组javascript创建对象数组 - How to create array of object from two different length of array javascript javascript-如何将数组中的每个元素与其他数组中的其他元素合并? - javascript - How to merge each element in array with other elements in different array? 我如何循环遍历数组中每个 object 的属性,每个 object 的属性都不同。 这是 Javascript - How do i loop through the properties of each object in the array, the properties are different in each object. This is Javascript 如果您不知道javascript中每个数组的长度,如何比较两个不同长度的数组? - how to compare two arrays of different length if you dont know the length of each one in javascript? 如何使用 javascript 按行长度过滤数组的 arrays? - How do I filter arrays of an array by row length using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM