最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to map an array vertically into columns? - Stack Overflow

programmeradmin3浏览0评论

If I have an array of items, such as,

const array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]

How can I map it, so that the screen/page renders,

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

I was able to get it to kind of work horizontally with,

    const chunkSize = 5;

    array
      .map((e, i) => {
        return i % chunkSize === 0 ?
           selected.slice(i, i + chunkSize)
         : null;
      })
      .filter(e => e);

But I am unable to get it to work vertically. How can I do this?

Edit:

The suggested solution from another answer returns subarrays, which is not what I had asked in this question.

If I have an array of items, such as,

const array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]

How can I map it, so that the screen/page renders,

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

I was able to get it to kind of work horizontally with,

    const chunkSize = 5;

    array
      .map((e, i) => {
        return i % chunkSize === 0 ?
           selected.slice(i, i + chunkSize)
         : null;
      })
      .filter(e => e);

But I am unable to get it to work vertically. How can I do this?

Edit:

The suggested solution from another answer returns subarrays, which is not what I had asked in this question.

Share Improve this question edited Nov 3, 2019 at 16:50 Mike K asked Nov 3, 2019 at 16:02 Mike KMike K 6,49117 gold badges75 silver badges143 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 10

You could calculate the index for the row.

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
    chunk = 5,
    result = array.reduce((r, v, i) => {
        (r[i % chunk] = r[i % chunk] || []).push(v);
        return r;
    }, []);
    

result.forEach(a => console.log(...a));

const array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]
const chunkSize = 5;

let result = [];
for (let i = 0; i < chunkSize; i++) {
    result[i] = [];
}

array.forEach((e,i) => {
    result[i % chunkSize].push(e);
});

console.log(result); 
/* 
Result : 
[ [ 1, 6, 11, 16 ],
  [ 2, 7, 12, 17 ],
  [ 3, 8, 13, 18 ],
  [ 4, 9, 14 ],
  [ 5, 10, 15 ] ]
*/

Here's a still-compact but readable version.

const columnize = (items, rows) => {
  const toColumns = (table, item, iteration) => {
    let row = iteration % rows;

    table[row] = table[row] || [];
    table[row].push(item);

    return table;
  };

  return items.reduce(toColumns, []);
};

Which would be used as:

const numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ];

console.log(columnize(numbers, 5));

https://jsfiddle.net/69fshprq/

Here is a way to output it as the questions asks. I'm not paying strict attention to the spacing, I'll leave that to a string padding function or technique to implement.

const numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]

// <pre id="out"></pre>
let out = document.getElementById('out')
let list = columnize(numbers, 5)

for (var column in list) {
    var item = list[column]
    var line = item.reduce((line, item) => line + item + '    ', '')

    out.textContent += line + ' \n'
}

https://jsfiddle.net/t60rfcpe/

发布评论

评论列表(0)

  1. 暂无评论