0

Basically I have an enum describing status

status = {1: "new", 2: "working" ... }

and I need to take it from that to something like

status = [{1: "new"}, {2: "working"} ...]

in a clean way ... i.e.

something readable, (maybe) simple and reliable. Currently I am not sure how to go about this ... maybe go through the object and "map" each pair to a brand new object in an array? Sounds bad.

1
  • You should add minimal reproducible code, so that people can understand your problem clearly. Commented Aug 24, 2021 at 6:09

3 Answers 3

2

Object.entries(data).map(([key, value]) => ({[key]: value})) The result of the map is the array, what you want. Or:

const arrayData = [];
for (const key in data) {
  arrayData.push({[key]: data[key]})
}
Sign up to request clarification or add additional context in comments.

Comments

0

iterate on objects dynamicly:

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

Comments

0

const { object } = require("prop-types")

const status = { 1: "new", 2: "working" }

const statusUpdate = Object.entries(status).map(([key, value]) => ({ [key]: value }))

console.log(statusUpdate);

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.