Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将一个数组分成两个数组_Javascript_Arrays_Sub Array - Fatal编程技术网

Javascript 将一个数组分成两个数组

Javascript 将一个数组分成两个数组,javascript,arrays,sub-array,Javascript,Arrays,Sub Array,我有一个带有n节点的数组(行)。每个节点包含6个元素 示例: rows[0]: ["1", "Text", "0", "55", "0", "Text 2"] rows[1]: ["2", "Another Text", "15.5", "55", "16.6", "Another Text 2"] ... rows[n]: [...] 我想将所有行[n]中的第二个元素(如[“Text”,“另一个Text”,…])放入单独的数组中 和 我想把第三个和第五个元素(如[[0,0],[15.5,16

我有一个带有n节点的数组(行)。每个节点包含6个元素

示例:

rows[0]: ["1", "Text", "0", "55", "0", "Text 2"]
rows[1]: ["2", "Another Text", "15.5", "55", "16.6", "Another Text 2"]
...
rows[n]: [...]
我想将所有行[n]中的第二个元素(如
[“Text”,“另一个Text”,…])
放入单独的数组中

我想把第三个和第五个元素(如
[[0,0],[15.5,16.6],…])也放入单独的数组中。

我想我需要
foreach
中的
foreach
您可以使用简单的
数组#map

const行=[
[“1”、“文本”、“0”、“55”、“0”、“文本2”],
[“2”、“另一文本”、“15.5”、“55”、“16.6”、“另一文本2”],
];
常量r=(d,n)=>rows.map((arr)=>arr[n]);
console.log(r(行,1));

console.log(r(行,2))
这是多种方法之一(它使用
数组#forEach


不是很漂亮,但我觉得更可读:

let row = [
    ["1", "Text", "0", "55", "0", "Text 2"],
    ["2", "Second Text", "15.5", "55", "16.6", "Second Text 2"],
    ["3", "Third Text", "17", "60", "18", "Third Text 2"]
];

let arrA = [], arrB = [];

function newArrA(el, arr) {
    arr.forEach(arrEl => arrA.push(arrEl[el-1]));
}

function newArrB(elA, elB, arr) {

    arr.forEach(arrEl => arrB.push( [ arrEl[elA-1], arrEl[elB-1] ] ) );

}

newArrA(2, row);
// arrA: ["Text", "Second Text", "Third Text"]

newArrB(3, 5, row);
// arrB[0]: ["0", "0"]
// arrB[1]: ["15.5", "16.6"]
// arrB[2]: ["17", "18"]

您可以转置矩阵并获取想要的列,然后再次转置以获得想要的结果

const
转置=(r,a)=>a.map((v,i)=>[…(r[i]| |[]),v]),
数组=[“1”、“文本”、“0”、“55”、“0”、“文本2”]、[“2”、“另一个文本”、“15.5”、“55”、“16.6”、“另一个文本2”],
columns=array.reduce(transpose,[]),
ad1=列[1],
ad2=[columns[2],columns[4]]。reduce(transpose,[]);
console.log(ad1);
console.log(ad2)

.as控制台包装{max height:100%!important;top:0;}
向我们展示您的尝试。使用各种不同的循环方法来实现这一点的方法很多。
rows.map((arr)=>arr[n])
?试试看。始终先尝试,然后向我们展示你的方法。通过这样做,你会学到更多,而不是复制/粘贴某些人不可避免地会给你的答案。看起来不错,但是
console.log(r(rows,1));log(r(行,2,4))不起作用
let row = [
    ["1", "Text", "0", "55", "0", "Text 2"],
    ["2", "Second Text", "15.5", "55", "16.6", "Second Text 2"],
    ["3", "Third Text", "17", "60", "18", "Third Text 2"]
];

let arrA = [], arrB = [];

function newArrA(el, arr) {
    arr.forEach(arrEl => arrA.push(arrEl[el-1]));
}

function newArrB(elA, elB, arr) {

    arr.forEach(arrEl => arrB.push( [ arrEl[elA-1], arrEl[elB-1] ] ) );

}

newArrA(2, row);
// arrA: ["Text", "Second Text", "Third Text"]

newArrB(3, 5, row);
// arrB[0]: ["0", "0"]
// arrB[1]: ["15.5", "16.6"]
// arrB[2]: ["17", "18"]