加入前两个元素并创建新数组?

时间:2018-08-18 10:57:13

标签: javascript arrays multidimensional-array

在使用join()slice()函数数小时之后,我发现您不能在复杂数组上使用这两个函数。所以我来这里得到一些帮助。

我正尝试在下面获取数据:

 var data = [
            ["North", "Tennis",37, 25, 11, 9, 42, 13],
            ["East", "Football", 41, 2, 3, 26, 47, 21],
            ["South", "Rugby", 7, 22, 35, 45, 11, 46],
            ["West", "Rugby", 30, 21, 44, 23, 4, 47],
            ["North East", "Football", 35, 27, 12, 39, 34, 13],
            ["North West", "Football", 23, 4, 41, 35, 9, 47]
        ];

看起来像这样(输出):

var result = [
    ["North: Tennis", 37, 25, 11, 9, 42, 13],
    ["East: Football", 41, 2, 3, 26, 47, 21],
    ["South: Rugby", 7, 22, 35, 45, 11, 46],
    ["West: Rugby", 30, 21, 44, 23, 4, 47],
    ["North East: Football", 35, 27, 12, 39, 34, 13],
    ["North West: Football", 23, 4, 41, 35, 9, 47]
];

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:3)

当您要将一个数组转换为另一个数组时,将每个子数组的前两个元素.map合并到另一个数组中,然后通过添加以下内容来构建新的子数组,这样可以将原始数组let data = [ ["North", "Tennis",37, 25, 11, 9, 42, 13], ["East", "Football", 41, 2, 3, 26, 47, 21], ["South", "Rugby", 7, 22, 35, 45, 11, 46], ["West", "Rugby", 30, 21, 44, 23, 4, 47], ["North East", "Football", 35, 27, 12, 39, 34, 13], ["North West", "Football", 23, 4, 41, 35, 9, 47] ]; let result = data.map(([s1, s2, ...rest]) => [`${s1}: ${s2}`, ...rest]); console.log(result);转换为另一个数组其余元素(我们在数组解构期间通过rest参数获得)到包含连接字符串的新数组。

.as-console-wrapper { max-height: 100% !important; top: 0; }
{{1}}

参考:

答案 1 :(得分:0)

我会写你的代码。

var data = [
  ["North", "Tennis", 37, 25, 11, 9, 42, 13],
  ["East", "Football", 41, 2, 3, 26, 47, 21],
  ["South", "Rugby", 7, 22, 35, 45, 11, 46],
  ["West", "Rugby", 30, 21, 44, 23, 4, 47],
  ["North East", "Football", 35, 27, 12, 39, 34, 13],
  ["North West", "Football", 23, 4, 41, 35, 9, 47]
];

// Final resulting array is created by concatenating the first and
// second index elements of each inner array of data. The rest data
// is being copied as it is using slice and spread operator.
const result = data.reduce(
  (mem, cur) => [...mem, [`${cur[0]}: ${cur[1]}`, ...cur.slice(2)]],
  []
);

console.log(result);

答案 2 :(得分:0)

如果我正确理解您的问题,那么您正在寻找以下输出。我已经使用JavaScript的.map()函数对数据进行了稍微的重组。 More information on .map here

var data = [
  ["North", "Tennis", 37, 25, 11, 9, 42, 13],
  ["East", "Football", 41, 2, 3, 26, 47, 21],
  ["South", "Rugby", 7, 22, 35, 45, 11, 46],
  ["West", "Rugby", 30, 21, 44, 23, 4, 47],
  ["North East", "Football", 35, 27, 12, 39, 34, 13],
  ["North West", "Football", 23, 4, 41, 35, 9, 47]
];

const formattedData = data.map(row => {

  return [
    [row[0] + ': ' + row[1]],
    row[2],
    row[3],
    row[4],
    row[5],
    row[6],
    row[7]
  ];
});

console.log(formattedData);

答案 3 :(得分:0)

您可以使用<html> <head> <title>Hello!</title> </head> <body onload="openPage()"> <a href="test4.php" id="def"></a> <a href="test5.php" ></a> <a href="test6.php" ></a> <script type="text/javascript"> function openPage(){ defaultLink=document.getElementById("def"); defaultLink=defaultLink.getAttribute("href"); window.location.href=defaultLink; } </script> </body> </html> 方法。它需要为每个元素执行的回调。然后,它返回一个 new 数组,其中每个索引中都有新数据。例如:

map