108

This is my Object

var data = {
    a:{"0": "1"},
    b:{"1": "2"},
    c:{"2": "3"},
    d:{"3": "4"}
};

This is the output that I expect

data = [ 
    {"0": "1"},
    {"1": "2"},
    {"2": "3"},
    {"3": "4"}
]
3
  • Then see my comment about the target structure. It's a bad output format and a pain to use in javascript. Why not just put all key/value pairs in a single object? Commented Nov 7, 2014 at 6:54
  • 1
    I am using a third party library in my application that needs the input in the format that i want. cant help it. Commented Nov 7, 2014 at 6:55
  • 2
    Wow, these comments make my head hurt. How is an array of objects "a pain to use in javascript"? Granted the objects are normally all the same structure, but we use arrays of objects all the time, quite painlessly. Commented Oct 31, 2017 at 17:07

12 Answers 12

143

This works for me

var newArrayDataOfOjbect = Object.values(data)

In additional if you have key - value object try:

const objOfObjs = {
   "one": {"id": 3},
   "two": {"id": 4},
};

const arrayOfObj = Object.entries(objOfObjs).map((e) => ( { [e[0]]: e[1] } ));

will return:

[
  {
    "one": {
      "id": 3
    }
  },
  {
    "two": {
      "id": 4
    }
  }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Note that this is not supported in IE, Safari,Opera
Object.values() not supported in FireFox
This doesn't return the data in the OP's specification. @Shuwei's answer works perfect.
typeof still returns 'object'
57
var data = {
    a:{"0": "1"},
    b:{"1": "2"},
    c:{"2": "3"},
    d:{"3": "4"}
};

var myData = Object.keys(data).map(key => {
    return data[key];
})

This works for me

5 Comments

This appears to be the best solution for all browser support.
Still IE of course has issues with the mapping. try instead: var myData = Object.keys(data).map(function(x) { return data[x]; });
how to revert that?
Simplified: let myData = Object.keys(data).map(key => data[key]);
This is the most accurate solution , thanks @JasonRice and Shuwei
11

The accepted answer doesn't take into account the OP wanted to get rid of the keys. This returns only the objects, not their parent key.

Object.entries(ObjOfObjs).map(e => e[1]) outputs:

[ 
  {"0": "1"},
  {"1": "2"},
  {"2": "3"},
  {"3": "4"}
]

Comments

8

You can easily do this by using Object.values() function

var data = {
    a:{"0": "1"},
    b:{"1": "2"},
    c:{"2": "3"},
    d:{"3": "4"}
};

console.log(Object.values(data))

2 Comments

this is simple way to convert
This should be the ACCEPTED answer, because this is the simplest, therefore the BEST way.
5

You would have to give a name to each value in the object.

Once you fix the first object, then you can do it using push.

var data = {
    1: {"0": "1"},
    2: {"1": "2"},
    3 : {"2": "3"},
    4: {"3": "4"}
};

var ar = [];
for(item in data){
    ar.push(data[item]);
 }

console.log(ar);

http://jsfiddle.net/nhmaggiej/uobrfke6/

1 Comment

I was thinking if there was another way rather than looping the whole Object. Because I have like a thousand records. But if there is nothing else that will work then I can use this. Thanks..
2

this is simple and will do in an immutable way so that your main data not touched but you can create a new mappedData as per your requirement and create a new array.

let data = {
  a: { "0": "1" },
  b: { "1": "2" },
  c: { "2": "3" },
  d: { "3": "4" }
};

const mappedDataArray = [];

for (const key in data) {
  const mappedData = {
    ...data[key]
  };
  mappedDataArray.push(mappedData);
}

console.log(mappedDataArray);
console.log(data);

Comments

1
var array = [];
for(var item in data){
    // this condition is required to prevent moving forward to prototype chain
    if(data.hasOwnProperty(item)){
        array.push(data[item]);
    } 
}

1 Comment

@jfriend00 Please see this question
1

Finally, It works for me to convert from object to array of objects when you get object value from

       const snapshortObject = snapshot.val(); //  And they look like this are stored value.

  let snapshortObject = {
      a: { 
            message: "Hiiii",
            senderId: "tQusPaBhoJXcu2ezlsDxQMUTLCq1",
            timestamp: "Wed Jun  8 18:16:18 2022"
      },
      b: { 
            message: "Hiiii",
            senderId: "tQusPaBhoJXcu2ezlsDxQMUTLCq1",
            timestamp: "Wed Jun  8 18:16:18 2022"
      },
      c: { 
            message: "Hello 👋👋👋👋",
            senderId: "tQusPaBhoJXcu2ezlsDxQMUTLCq1",
            timestamp: "Wed Jun  8 18:16:50 2022"
      },
      d: { 
            message: "Hello 👋👋👋👋",
            senderId: "tQusPaBhoJXcu2ezlsDxQMUTLCq1",
            timestamp: "Wed Jun  8 18:16:50 2022"
      }
    };
    
    const mappedDataArray = [];
    slno=1
    for (const key in snapshortObject) {
      const mappedData = {
        ...snapshortObject[key],
        slno:slno++
      };
      mappedDataArray.push(mappedData);
    }
    console.log(mappedDataArray);

1

Comments

1

Easy Hack

  var data = {
        a:{"0": "1"},
        b:{"1": "2"},
        c:{"2": "3"},
        d:{"3": "4"}
    };

 data = Object.values(data).map((item)=>item)
console.log(data)

Comments

0

I get what you want ! Here is your solution,

var dataObject=[{name:'SrNo',type:'number'}];

And to access or store the array use

dataObject[0].srno=1;
dataObject[0].srno=2;

Hope this is what you needed.

2 Comments

That is correct, because i have used it in my code. You try it then you will get it!
This does not provide any answer to the OP question, This codes only add a new property to the object dataObject[0]
0

This worked for me. And it seems to be well supported.

toArray(obj_obj) {
    return Object.keys(obj_obj).map(i => obj_obj[i]);
}

https://medium.com/chrisburgin/javascript-converting-an-object-to-an-array-94b030a1604c

Comments

0

We can use Object default methods (Object.assign(), Object.values()) to convert Object of Object to array in the following way

const ObjValue = {
a:{"0": "1"},
b:{"1": "2"},
c:{"2": "3"},
d:{"3": "4"}
}
const ObjtoArray = (obj) => Object.assign([], Object.values(obj))

console.log(ObjtoArray(ObjValue))

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.