0

what I have is like this

const employees = {
  "0813562121": {
    "city": "Melbourne",
    "name": "Allison"
  },
  "8753452122": {
    "city": "Melbourne",
    "name": "Aria"
  }
}

I need to make it into like this

 const employees = [
     {
        "city": "Melbourne",
        "name": "Allison",
        "_id": "0813562121"
      },
      {
        "city": "Melbourne",
        "name": "Aria",
        "_id": "8753452122"
      }
    ]

i was thinking of using index of Object.keys(employees ) and Object.values(employees ), but I couldn't figure it out.

2 Answers 2

1

You're on the right track with Object.values, but I'd use Object.entries since it gives you both the property name and its value. Then it's a map:

const result = Object.entries(employees).map(([name, value]) => ({...value, _id: name}));

Live Example:

const employees = {
  "0813562121": {
    "city": "Melbourne",
    "name": "Allison"
  },
  "8753452122": {
    "city": "Melbourne",
    "name": "Aria"
  }
};

const result = Object.entries(employees).map(([name, value]) => ({...value, _id: name}));

console.log(result);

That uses:

  • Object.entries to get an array of [name, value] pairs from the object.
  • map to map the entries of that array into a different format
  • A concise-form arrow function for the map callback (more in a moment).
  • Destructuring in the map callback's parameter list to get the name and value in named parameters.
  • Spread syntax inside an object literal to spread out the own, enumerable properties of the objects into a new object, adding the _id property

Alternatively, you could modify the object rather than creating a new one, so that non-enumerable, or non-own properties are retained:

const result = Object.entries(employees).map(([name, value]) => {
    value._id = name;
    return value;
});

Live Example:

const employees = {
  "0813562121": {
    "city": "Melbourne",
    "name": "Allison"
  },
  "8753452122": {
    "city": "Melbourne",
    "name": "Aria"
  }
};

const result = Object.entries(employees).map(([name, value]) => {
    value._id = name;
    return value;
});

console.log(result);

The key thing there is that the same objects are in both employees and result, whereas with the first one we make a shallow copy.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

const employees = {
  "0813562121": {
    "city": "Melbourne",
    "name": "Allison"
  },
  "8753452122": {
    "city": "Melbourne",
    "name": "Aria"
  }
}

let jsonVariants =[];
Object.keys(employees).forEach(function(key) {
   let obj =employees[key];
   obj["_id"] =key;
   jsonVariants.push(obj); 
});

console.log(jsonVariants);

2 Comments

Code dumps aren't generally useful answers. Say what you did, and why. Also, your code is falling prey to what I call The Horror of Implicit Globals (you need to declare obj), and generally if you're using forEach but always pushing to a new array, map is a more idiomatic option. But great to do a Stack Snippet! :-)
@T.J.Crowder Thanks for showing my mistakes.

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.