Shaun
Shaun

Reputation: 471

How to store object values by getting the key dynamically?

Using the following array of objects, how do I just get the values for each of the key and save it into the desired name and value structure format mentioned below?

{
  list: [
    [
      {
        Desc: {
          value: '7777 - Florida Hurricane'
        },
        DSTR_NR: {
          value: '7777'
        }
      },
      {
        ST_NM: {
          value: 'Alaska'
        },
        ST_CD: {
          value: 'AK'
        }
      },
      {
        Desc: {
          value: '7172 - Virginia Severe Storm(s)'
        },
        DSTR_NR: {
          value: '7172'
        }
      },
      {
        ST_NM: {
          value: 'Florida'
        },
        ST_CD: {
          value: 'FL'
        }
      },
      {
        Desc: {
          value: '7002 - Maryland Hurricane'
        },
        DSTR_NR: {
          value: '7002'
        }
      },
      {
        ST_NM: {
          value: 'Louisiana'
        },
        ST_CD: {
          value: 'LA'
        }
      }
    ]
  ]
}

Desired format that I need regardless if the api returns disaster related data or states related data. I just need to store the data values in name/value pair like the following:

{
  list: [
    {
      name: '7777',
      value: '7777 - Florida Hurricane'
    },
    {
      name: 'AK',
      value: 'Alaska'
    },
    ...
  ]
}

Here's my current code:

let newData = {}
  list.forEach((value) => {
    Object.keys(value).forEach((key) => {
      if(newData[key] === undefined){
        newData[key] = [value[key]]
      } else {
        newData[key].push(value[key])
      }
    })
  });

Upvotes: 1

Views: 63

Answers (2)

Areg
Areg

Reputation: 1485

You can map through the object like so

const res = {
  list: [
    [
      {
        Desc: {
          value: '7777 - Florida Hurricane'
        },
        DSTR_NR: {
          value: '7777'
        }
      },
      {
        ST_NM: {
          value: 'Alaska'
        },
        ST_CD: {
          value: 'AK'
        }
      },
      {
        Desc: {
          value: '7172 - Virginia Severe Storm(s)'
        },
        DSTR_NR: {
          value: '7172'
        }
      },
      {
        ST_NM: {
          value: 'Florida'
        },
        ST_CD: {
          value: 'FL'
        }
      },
      {
        Desc: {
          value: '7002 - Maryland Hurricane'
        },
        DSTR_NR: {
          value: '7002'
        }
      },
      {
        ST_NM: {
          value: 'Louisiana'
        },
        ST_CD: {
          value: 'LA'
        }
      }
    ]
  ]
}

const list = res.list[0];

const format = list.map(i => {
  const items = Object.values(i);
  const newItem = {}
  newItem.name = items[1].value;
  newItem.value = items[0].value;
  
  return newItem
})

console.log(format)

Upvotes: 3

Isaac Vega
Isaac Vega

Reputation: 330

If the data contains always the value first and the name second, you can do this:

// let response = { list: [...] }
let result = {
  list: []
};

response.list.forEach(sublist => {
  sublist.forEach(elem => {
    let keys = Object.keys(elem);
    result.list.push({
      name: elem[ keys[1] ].value,
      value: elem[ keys[0] ].value,
    })
  });
})

console.log("RESULT: ", result);

Upvotes: 1

Related Questions