JavaScript JavaScript - Return object key and value when both are strings javascript return object from function,javascript return object literal,javascript

So, I have this array of objects:

var obj = [{
    "Has the house been sold?": "N"
}, {
    "Is the house on the market?": "Y"
}, {
    "Duration of Sale": "2 weeks"
}];

and I am trying to turn it so that it key and value are like this:

var obj = [
    {key: 'Has the house been sold?', value: 'N'}
];

But I cannot find a way to grab the key text because it only gives me the index.

for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
        var val = obj[key];
        console.log(val);
    }
}

Can someone help me out here? What am I missing?

Answer:1

You could use Array#map and Object.keys for own properties.

var obj = [{ "Has the house been sold?": "N" }, { "Is the house on the Market?": "Y" }, { "Duration of Sale": "2 weeks" }],
array = obj.map(function (a) {
    var key = Object.keys(a)[0];
    return { key: key, value: a[key] };
});
console.log(array);
Answer:2

Use Array.map().

var obj = [{
    "Has the house been sold?": "N"
}, {
    "Is the house on the market?": "Y"
}, {
    "Duration of Sale": "2 weeks"
}];

var newObj = obj.map(function(ea, i){
    var thisKey = Object.keys(ea)[0];
    return {key: thisKey, value: ea[thisKey]};
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Answer:3

You need to loop through each item in obj since it is an array, and then for each item, loop through its properties and then save them.

var obj = [{
    "Has the house been sold?": "N"
}, {
    "Is the house on the market?": "Y"
}, {
    "Duration of Sale": "2 weeks"
}];

var newObj = [];

for (var i = 0; i < obj.length; i++) {
  for (var key in obj[i])
  {
    newObj.push({key: key, value: obj[i][key]})
  }
}
console.log(JSON.stringify(newObj))
Answer:4

Consider the following example snippet of my code. I have tried parseInt with no luck. Here, I want javascript to set a number in a cell PICweight then retrieve it later and place it in a different ...

Consider the following example snippet of my code. I have tried parseInt with no luck. Here, I want javascript to set a number in a cell PICweight then retrieve it later and place it in a different ...

  1. getelementbyid returns null
  2. getelementbyid returns undefined
  3. getelementbyid returns null react
  4. getelementbyid returns null javascript
  5. getelementbyid returns null angular
  6. getelementbyid() returns null for dynamic objects
  7. getelementbyid returns object htmlinputelement
  8. getelementbyid returns object
  9. getelementbyid returns string
  10. getelementbyid returns nothing
  11. getelementbyid returns multiple
  12. getelementbyid returns empty
  13. getelementbyid returns value
  14. getelementbyid returns reference
  15. document.getelementbyid returns null
  16. document.getelementbyid returns undefined
  17. document.getelementbyid returns null angular
  18. js getelementbyid returns null
  19. getelementbyid().value returns undefined
  20. document.getelementbyid returns null react

I'm trying to make a JavaScript search for a div within the same page. I'm currently using this code to display my search query: $('.form-search').on('submit',function(){return false;}); $('.form-...

I'm trying to make a JavaScript search for a div within the same page. I'm currently using this code to display my search query: $('.form-search').on('submit',function(){return false;}); $('.form-...

I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's ...

I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's ...

  1. keep chrome extension open
  2. keep chrome extension corrupted
  3. keep chrome extension
  4. keep chrome extension android
  5. google keep chrome extension
  6. keep awake chrome extension
  7. keep awake chrome extension and install
  8. google keep chrome extension not working
  9. keep notes chrome extension
  10. google keep chrome extension page
  11. google keep chrome extension android
  12. keep alive chrome extension
  13. keep private chrome extension
  14. google keep chrome extension download
  15. keep up chrome extension
  16. keep tabs chrome extension
  17. keep key chrome extension
  18. keep safe chrome extension
  19. keep song chrome extension
  20. keepvid chrome extension

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

  1. object defineproperty not working
  2. object fit property not working in ie
  3. object fit property not working