0

I am fetching data from different sources and have ended up with a several correctly formed json objects and one which has parent keys like below:

{
"0": {
      "term_id": 3,
      "name": "Burger"
     },
"1": {
      "term_id": 6,
      "name": "Chicken"
     },
"2": {
      "term_id": 12,
      "name": "Mexican"
     },
}

How can I remove 0, 1, 2, 3 etc while also preserving other correctly structured objects? I am using lodash elsewhere in this project

This object with parent keys is being inserted into an array of multiple objects via a map

Promise.all(promises)
  .then(results => {
    let valueArr = [];
    Object.keys(results).forEach(function(key) {
      valueArr = [results[key]]
    });
    this.setState({ categorySelectOptions: valueArr });
  })

This is a screenshot of the output with the problem:

2 Answers 2

1

All you need to do is push into the array instead of assigning. Use this -

Object.keys(results).forEach(function(key) {
  valueArr.push(results[key])
});
Sign up to request clarification or add additional context in comments.

1 Comment

It still pushes the object with the parent keys as in the screenshot above. Thanks
0

Use Object.values

const data = {
  "0": {
    "term_id": 3,
    "name": "Burger"
  },
  "1": {
    "term_id": 6,
    "name": "Chicken"
  },
  "2": {
    "term_id": 12,
    "name": "Mexican"
  },
};

console.log(Object.values(data));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.