JavaScript Is there an easy way to convert a Javascript Object into “name”: “Fred”, “value”: “Blue” style pairs?

I'm positive this question must have been covered before, but I can quite find it. So....

I have an object like so

Object
name: Fred
lastname: Jones
city: Los Angeles

I'd like to use Javascript to convert it to a string that looks like this:

//Do want this
[
 {"name": "name", "value": "Fred"},
 {"name": "lastname", "value": "Jones"},
 {"name": "city", "value": "Los Angeles"}
]

All of the examples I've found use JSON.parse() to get a result that looks like this (which I don't want):

//Don't want this
[
 {"name": "Fred", "lastname": "Jones", "city": "Los Angeles"}
]

I'm working with another developer who says this is how Jquery parses objects (EDIT- he's using $serializeArray(), so perhaps JQuery has a method to help me with this.

Any ideas would be most welcome.

Thanks!

Answer:1

This conversion calls for iterating through the properties of the source object and accumulating entries in a result array.

function toList( obj ) {
  var rv = [], k;
  for (k in obj) {
    if (obj.hasOwnProperty(k))
      rv.push({ name: k, value: obj[k] });
  }
  return rv;
}

var list = toList( myObject );
Answer:2
var arr = []
for (var key in object_name) {
    arr.push({'name': key, 'value': object_name[key]})
}

Loop through the keys and add it to the array.

Answer:3

You can loop through the object properties and create the array

var a = array();
for (p in obj) {
    a.push({'name': p, 'value': obj[p]});
}

This should get the structure that you want.

Answer:4

I have an AngularJS animation... app.animation('slide-show', function () { return { setup: function (element) { }, start: function (element, done) { element....

I have an AngularJS animation... app.animation('slide-show', function () { return { setup: function (element) { }, start: function (element, done) { element....

I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the ...

I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the ...

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...