Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Turning a 1D array and a variable into a new 2D array

Im new to programming and Im struggling with arrays in typescript.
I have the following variables:

let nameList: string[] = ['a', 'b', 'c', 'd'];
    let postId: number = 1;

My goal is to create a new 2D array:
let newList: (string | number)[][] = [];

where

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

newlist = [
      [a,1],
      [b,1],
      [c,1],
      [d,1]
    ]

I’ve tried:

for (let i: number = 0; i < userList.length; i++) {
      newList[i][0].push(userList[i]);
      newList[i][1].push(insertID);
    }

Also

for (let i: number = 0; i < userList.length; i++) {
      newList[i][0] = userList[i];
      newList[i][1] = insertID;
    }

Can someone help me with this?
Much apprerciated!

>Solution :

const new2dArray: [string, number][] = nameList.map((name) => [name, postId]);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading