如何修剪多维数组中的值

how to trim values from multidimentional array

提问人:Srinivas Rao 提问时间:5/18/2021 最后编辑:Mister JojoSrinivas Rao 更新时间:5/19/2021 访问量:136

问:

    console.log(csvcontents.data.length - 8);
        for (let i = 7; i < csvcontents.data.length - 1; i++) {
          // console.log(csvcontents.data[i][0]);
          // arrayListFromCsvFile.push(csvcontents.data[i]);
           arrayListFromCsvFile.push(csvcontents.data[i]);
        }
        console.log(arrayListFromCsvFile); 
      }

此代码打印输出,如下所述

[
  [
    '2021-05-18',
    '17:07:32',
    'Informational',
    'abc',
    'xyz',
    "Web user 'abc' logged in ",
    '0x00'
  ],
[
    '2021-05-18',
    '17:07:32',
    'Informational',
    'xyz',
    'abc',
    "Web user 'abc' logged in ",
    '0x10'
  ],
]

有没有办法删除索引从 开始的调用值。
即,我只想在两个数组中都使用 , ,,
2,3,62021-05-18'17:07:32''abc''Web user 'abc' logged in

JavaScript AngularJS 多维数组 量角器 切片

评论

0赞 Mister Jojo 5/18/2021
看看阵列拼接 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
0赞 bel3atar 5/18/2021
我们没有看到任何.输入是什么样子的?2,3,6
0赞 Srinivas Rao 5/19/2021
谢谢@MisterJojo。成功了

答:

0赞 DecPK 5/18/2021 #1

您可以解构数组,将相应的索引数据转换为变量,并将其包含在结果数组中。

function run() {
  console.log(csvcontents.data.length - 8);
  for (let i = 7; i < csvcontents.data.length - 1; i++) {
    const [a, b, c, d, e, f, g] = csvcontents.data[i];
    arrayListFromCsvFile.push([a, b, d, f]);
  }
  console.log(arrayListFromCsvFile);
}

例如

const arr = [0, 1, 2, 3, 4, 5, 6];
const result = [];
const [a, b, c, d, e, f, g] = arr;
result.push([a, b, d, f]);
console.log(result);

1赞 Akhil M 5/18/2021 #2

你可以试试这个。

    console.log(csvcontents.data.length - 8);

    //iterate through each data and return an new array that has all the required elements in array.

    const arrayListFromCsvFile = csvcontents.data.map((data) => {
       return [data[0], data[1], data[3], data[5]]
    })
    console.log(arrayListFromCsvFile); 

评论

0赞 XouDo 5/18/2021
您能详细说明为什么这应该起作用以及它与其他答案有什么不同吗?
1赞 Akhil M 5/18/2021
@XouDo 在这里,我们可以遍历数组上的每个元素并返回具有我们需要的特定元素的新数组。如果我们要使用析构,则有一些变量被分配但未使用。
1赞 Mister Jojo 5/18/2021 #3

看看 array.splice()

const data = 
 [ [ '2021-05-18'
    , '17:07:32'
    , 'Informational'
    , 'abc'
    , 'xyz'
    , "Web user 'abc' logged in "
    , '0x00'
    ] 
  , [ '2021-05-18'
    , '17:07:32'
    , 'Informational'
    , 'xyz'
    , 'abc'
    , "Web user 'abc' logged in "
    , '0x10'
  ] ] 
      
      
data.forEach(row=>
  {
  row.splice(6,1) // start with the last one,
  row.splice(3,1) // otherwise calculate with the offset 
  row.splice(2,1)
  })
  
console.log( data )
.as-console-wrapper {max-height: 100%!important;top:0}

评论

0赞 Srinivas Rao 5/19/2021
是的,谢谢我使用了 console.log(csvcontents.data.length - 8);for (let i = 7; i < csvcontents.data.length - 1; i++) { // console.log(csvcontents.data[i][0]); // arrayListFromCsvFile.push(csvcontents.data[i]); csvcontents.data[i].splice(2, 1); // 0 1 2 3 4 5 //2 3 5 csvcontents.data[i].splice(2, 1); csvcontents.data[i].splice(4, 1); arrayListFromCsvFile.push(csvcontents.data[i]); } console.log(arrayListFromCsvFile);这奏效了,谢谢
0赞 Ashish Mehta 5/18/2021 #4

function modifiedArray (array){
      return array.map( ele =>  {
         return ele.filter( (el, i) => {
           return ![2,3,6].includes(i) ;
         })
      })
    }
    const data = [
          [
            '2021-05-18',
            '17:07:32',
            'Informational',
            'abc',
            'xyz',
            "Web user 'abc' logged in ",
            '0x00'
          ],
          [
            '2021-05-18',
            '17:07:32',
            'Informational',
            'xyz',
            'abc',
            "Web user 'abc' logged in ",
            '0x10'
          ],
    ]
console.log(modifiedArray(data))

-1赞 Srinivas Rao 5/19/2021 #5
console.log(csvcontents.data.length - 8);// actual data starts from 8th row.Hence considering the count of lenght from 8th row
    for (let i = 7; i < csvcontents.data.length - 1; i++) {
      // console.log(csvcontents.data[i][0]);
      // arrayListFromCsvFile.push(csvcontents.data[i]);

      csvcontents.data[i].splice(2, 1);  // 0 1 2 3 4 5    //2 3 5
      csvcontents.data[i].splice(2, 1);
      csvcontents.data[i].splice(4, 1);
      arrayListFromCsvFile.push(csvcontents.data[i]);
    }
    console.log(arrayListFromCsvFile);

使用 splice() 后工作。