简体   繁体   中英

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?

Your desird outcome is not valid 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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