1

I am looking for a way to transform an object into an array of objects, and remove the first unique key.

How can I make this:

{f56hdhgf54: {name: 'Sam', age: 34}, h65fg9f7d: {name: 'John', age: 42}}

into this:

[{name: 'Sam', age: 34}, {name: 'John', age: 42}]

so I can .map through it like this:

result.map((person) => {
   console.log(person.name, person.age)
})
1
  • Check Object.values Commented Jul 21, 2016 at 15:46

3 Answers 3

5

You can use Object.keys() to get array of keys and then map() to change keys to values or in this case objects.

var obj = {f56hdhgf54: {name: 'Sam', age: 34}, h65fg9f7d: {name: 'John', age: 42}}

var result = Object.keys(obj).map(function(e) {
  return obj[e];
});

console.log(result);

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

1 Comment

Perfect. Thank you! :)
0

Another approach will be using lodash map function to transform the data, i.e

let data = {
   "f56hdhgf54": {
      "name": "Sam",
       "age": 34
    },
    "h65fg9f7d": {
      "name": "John",
      "age": 42
    }
}


let result = _(data)
  .map((value, key) => (
    value
  ))


 // result
 [
   {
      "name": "Sam",
      "age": 34
   },
   {
      "name": "John",
      "age": 42
   }
]

You can use the Lodash Online tester https://codepen.io/travist/full/jrBjBz to test the code

Comments

-1

Using Jquery solution of this question

var xyz = [];
    var abcd = {f56hdhgf54: {name: 'Sam', age: 34}, h65fg9f7d: {name: 'John', age: 42}}
    $.each(abcd,function(i,data){

    xyz .push(data);

    })

5 Comments

This is jQuery? I can't use that :)
Then You can iterate using for loop i am habitual of using jquery iterator
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@MichaelParker Yes you are right actually i feel easiness in code to understanding where jquery simplifies lot of complex code in javascript
What I mean is that your answer only gives code, but not an explanation for why this code works. Code-only answers are considered to be low-quality here on StackOverflow.

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.