将一个一维数组和一个变量转换为一个新的二维数组。

huangapple go评论88阅读模式
英文:

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

问题

我是新手程序员,对 TypeScript 中的数组感到困惑。
我有以下变量:

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

我的目标是创建一个新的二维数组:
let newList: (string | number)[][] = [];

其中

  1. newlist = [
  2. [a,1],
  3. [b,1],
  4. [c,1],
  5. [d,1]
  6. ]

我尝试过:

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

还尝试过

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

有人能帮我解决这个问题吗?
非常感谢!

英文:

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

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

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

where

  1. newlist = [
  2. [a,1],
  3. [b,1],
  4. [c,1],
  5. [d,1]
  6. ]

I've tried:

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

Also

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

Can someone help me with this?
Much apprerciated!

答案1

得分: 0

const new2dArray: [string, number][] = nameList.map((name) => [name, postId]); 的中文翻译是:

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

英文:

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

huangapple
  • 本文由 发表于 2023年4月19日 17:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定