Capture key and value in .map

Not sure what I'm missing here:

export function extractOptions(options){
  return options.map((option, i) => {
    if(!option.value){
      logRequiredOptionsMissingError(option.key)
      process.exit(1)
      return
    }
  })
}

      

Given this array as parameters:

[{ branch: 'A15'},
 { awsKey: 'AKIAQ'},
 { awsSecret: '0ro'}]

      

I am getting undefined for .value

. And yes, there is data there, I can see it when I debug.

Basically I want it to go through and check if each key has a value ... otherwise log an error and stop if it encounters a key with no value

UPDATE

I ended up going with this:

export function extractOptions(options){
  for (let option of options) {
    if(!option || option.length < 1){
      logRequiredOptionsMissingError(option[0][0])
      process.exit(1)
      return
    }
  }

  return options
}

      

As an added bonus, I will share my test

it('extracts options', () => {
      const options = [
        { branch: 'A15'},
        { awsKey: 'AKIAQ'},
        { awsSecret: '0ro'}
      ],
        list = Deploy.extractOptions(options)

      expect(list).to.deep.equal(options)
 })

      

+3


source to share


1 answer


You can get key value pairs as an array from an object with Object.entries()

return options.map((option, i) => {
    var data = Object.entries(option)
    console.log(data[0][0], data[0][1]) // will be your key and value
    if(!data[0][1]){
      logRequiredOptionsMissingError(data[0][0])
      process.exit(1)
      return
    }
  })

      



Object.entries () documentation

+1


source







All Articles