简体   繁体   English

从javascript中的对象数组中删除键

[英]Delete keys from an array of objects in javascript

I have an array of objects as shown below我有一个对象数组,如下所示

[{
    '0': 'RECORD_KEY',
    '1': 'FIRST_TOUCH_DATE_TIME',
    '2': 'ISA_SENDER_ID'
  },
  {
    '0': '00208851228_1',
    '1': '2020-02-19 13:08:20.0',
    '2': 'CCA'
  }, {
    '0': ''
  }
]

I want to remove the key and the last object which is null.我想删除键和最后一个为空的对象。

So the result would be -所以结果是——

[{
    '0': 'RECORD_KEY',
    '1': 'FIRST_TOUCH_DATE_TIME',
    '2': 'ISA_SENDER_ID'
  },
  {
    '0': '00208851228_1',
    '1': '2020-02-19 13:08:20.0',
    '2': 'CCA'
  }
]

So each object would represent a row of data.所以每个对象都代表一行数据。 How can I possibly do this in node.js?我怎么可能在 node.js 中做到这一点?

Your desird outcome is not valid JS.你想要的结果不是有效的 JS。 What you need is a nested array(array of arrays):你需要的是一个嵌套数组(数组数组):

const arr =[
  {
    '0': 'RECORD_KEY',
    '1': 'FIRST_TOUCH_DATE_TIME',
    '2': 'ISA_SENDER_ID'
  },
  {
    '0': '00208851228_1',
    '1': '2020-02-19 13:08:20.0',
    '2': 'CCA'
  },
  { '0': '' }
]

const mainArr = []

for(let item of arr){ 
  const subArr = []
  for(let prop in item){
    if(!item[prop]){
      continue;
    }
    subArr.push(item[prop])
  }
  if(subArr.length){
    mainArr.push(subArr)
  }
}

One note: Please next time provide a formatted code snippet注意事项:请下次提供格式化的代码片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM