0

This is my Code

"type":{"0": { "label":"name", "required":false, "type":"String", }, "1": { "label":"email", "required":false, "type":"String", } } 

In the above code I have Type object which contains two nested objects. Now I want to convert that object to array of objects in the following format using angularjs.

OutPut should be like this:-

"type":[ { "label":"name", "required":false, "type":"String", }, { "label":"email", "required":false, "type":"String", } ] 
2

7 Answers 7

1

Something like this perhaps?

var arr = []; Object.keys(obj).forEach(function(key) { arr.push(obj[key]); }); obj = { type: arr };

2
  • You have missed that the output should be { type: [...] }CommentedApr 7, 2016 at 9:40
  • ThankYou @Tom Mettam.Its WorkingCommentedApr 7, 2016 at 9:56
1

You should do this using Object.keys

objsArray = []; Object.keys(objs).forEach(function(key) { objsArray.push(objs[key]); }); 
1
  • You too have missed that the output should be { type: [...] }CommentedApr 7, 2016 at 9:41
1

You could use the keys as index of the array.

var object = { "type": { "0": { "label": "name", "required": false, "type": "String", }, "1": { "label": "email", "required": false, "type": "String", } } }, array = []; Object.keys(object.type).map(function (k) { array[+k] = object.type[k]; }); object.type = array; document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

1
  • 1
    You have missed that the output should be { type: [...] }CommentedApr 7, 2016 at 9:41
0

say you have

var obj = {"type":{"0": { "label":"name", "required":false, "type":"String", }, "1": { "label":"email", "required":false, "type":"String", } }} 

you can do

var arr = []; for(var key in obj.type){ arr.push(obj.type[key]) } obj.type = arr; 
2
  • You have missed that the output should be { type: [...] }CommentedApr 7, 2016 at 9:40
  • ya, that's why I have written obj.type = arr; which will replace old object with arrayCommentedApr 7, 2016 at 9:43
0

Let's say your object was:

var myObj = "type":{"0": { "label":"name", "required":false, "type":"String", }, "1": { "label":"email", "required":false, "type":"String", } }; 

If you're using Lodash library:

myObj.type = _.map(myObj.type) 
    0

    For the sake of joining the party - better late than never. Working with arrays using functional programming is lots of fun and the accepted answer is great but a slight tweak and you got what is in my opinon much cleaner code.

     obj = { type: Object.keys(obj).map(function(key) { return obj[key]; }) }; 

    or using a cleaner more sucinct ES6 steez

     obj = { type: Object.keys(obj).map(key => obj[key]) }; 

    Super clean!

      0

      You can do it easily without loops:

      // Assuming Problem: var objectToArray = { "type":{"0": {"label":"name", "required":false, "type":"String", }, "1": { "label":"email", "required":false, "type":"String", } } } // Answer: console.log( {type : Array.from(Object.values(objectToArray.type))} ); 
      1
      • Object.values() returns an array, you don't need to call Array.from()
        – Barmar
        CommentedMay 11, 2023 at 17:23

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.