TransWikia.com

Nested array into array, adding brackets around nested array

Stack Overflow Asked by Stackaccount1 on December 1, 2021

Cannot find anything on this, there is a lot of information on appending string values using push or typing out the whole array using push; but not using a nested array by the array variable name. Just need one pair of [] around my user inputed values.


var rows = []
var cols = []

rows after user input:

Array(4)
0: (4) ["ROW1", "ROW1", "ROW1", "ROW1"]
1: (4) ["ROW2", "ROW2", "ROW2", "ROW2"]
2: (4) ["ROW3", "ROW3", "ROW3", "ROW3"]
3: (4) ["ROW4", "ROW4", "ROW4", "ROW4"]

cols.push = ([rows])

Desired output is just

cols = [["ROW1", "ROW1", "ROW1", "ROW1"]
  ["ROW2", "ROW2", "ROW2", "ROW2"]
  ["ROW3", "ROW3", "ROW3", "ROW3"]
  ["ROW4", "ROW4", "ROW4", "ROW4"]]

Or this

cols = [["ROW1", "ROW1", "ROW1", "ROW1"],
  ["ROW2", "ROW2", "ROW2", "ROW2"],
  ["ROW3", "ROW3", "ROW3", "ROW3"],
  ["ROW4", "ROW4", "ROW4", "ROW4"]]

To be reproduced
I believe this is how data is represented from chrome after input

var rows = []
var cols = []

rows = ["ROW1", "ROW1", "ROW1", "ROW1"]
  ["ROW2", "ROW2", "ROW2", "ROW2"]
  ["ROW3", "ROW3", "ROW3", "ROW3"]
  ["ROW4", "ROW4", "ROW4", "ROW4"]

cols.push = ([rows])

Full Code

'use strict';

var items = []
var rows = []
var cols = []

function myFunction() {
    items.push(document.getElementById('itemid').value);
    items.push(document.getElementById('itemid1').value);
    items.push(document.getElementById('itemid2').value);
    items.push(document.getElementById('itemid3').value);
    console.log(items);
    const arrayToMatrix = (array, columns) => Array(Math.ceil(array.length / columns)).fill('').reduce((acc, cur, index) => {
        return [...acc, [...array].splice(index * columns, columns)]
    }, []);
    const result = arrayToMatrix(items, 4);
    var rows = result;
    console.log(rows);}

document.getElementById('my-Function').onclick = myFunction;

function exportData() {
    cols.push = ([rows]);
    console.log(cols);
    let csvContent = "data:text/csv;charset=utf-8," 
        + rows.map(e => e.join(",")).join("n");

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "my_data.csv");
    document.body.appendChild(link);
    link.click();}

document.getElementById('export-Data').onclick = exportData;

One Answer

I think what you're looking for is this:

var cols = [];
var rows;

rows = ["ROW1", "ROW1", "ROW1", "ROW1"];
cols.push(rows);
rows = ["ROW2", "ROW2", "ROW2", "ROW2"];
cols.push(rows);
rows = ["ROW3", "ROW3", "ROW3", "ROW3"];
cols.push(rows);
rows = ["ROW4", "ROW4", "ROW4", "ROW4"];
cols.push(rows);

console.log(cols);

You don't need to use [rows] -- rows is already an array, and push() creates the containing array.

The value of rows can come from user input.

let cols = [];

while (true) {
  let input = prompt("Enter words, blank line or Cancel when done");
  if (!input) {
    break;
  }
  let rows = input.split(" ");
  cols.push(rows);
}
console.log(cols);

Answered by Barmar on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP