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 };

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

2 Comments

You have missed that the output should be { type: [...] }
ThankYou @Tom Mettam.Its Working
1

You should do this using Object.keys

objsArray = [];
Object.keys(objs).forEach(function(key)
{
    objsArray.push(objs[key]);
});

1 Comment

You too have missed that the output should be { type: [...] }
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 Comment

You have missed that the output should be { type: [...] }
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 Comments

You have missed that the output should be { type: [...] }
ya, that's why I have written obj.type = arr; which will replace old object with array
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)

Comments

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!

Comments

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 Comment

Object.values() returns an array, you don't need to call Array.from()

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.