【问题标题】:how to trim values from multidimentional array如何从多维数组中修剪值
【发布时间】:2021-08-07 15:33:42
【问题描述】:
    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,6开始的调用值。
即,我只想要2021-05-18'17:07:32','abc','Web user 'abc' logged in 在两个数组中

【问题讨论】:

标签: javascript angularjs multidimensional-array protractor slice


【解决方案1】:

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}

【讨论】:

  • 是的,谢谢我使用了 console.log(csvcontents.data.length - 8); for (let i = 7; i
【解决方案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); 

【讨论】:

  • 你能详细说明为什么这应该起作用以及它与其他答案的不同之处吗?
  • @XouDo 在这里我们可以遍历数组中的每个元素并返回包含我们需要的特定元素的新数组。如果我们要使用破坏,则有一些变量已分配但未使用。
【解决方案3】:

您可以解构数组,将相应的索引数据放入变量中,并将其包含到结果数组中。

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);

【讨论】:

    【解决方案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))

    【讨论】:

      【解决方案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() 后工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多