1

Let's say I have an array like this

var myarray=[{"id":1234, "listtext":open, "prop3":value3 ,"prop4": value4},
             {"id":1235, "listtext":open, "prop3":value3 ,"prop4": value4},
             {"id":1236, "listtext":open, "prop3":value3 ,"prop4": value4},
             {"id":1237, "listtext":open, "prop3":value3 ,"prop4": value4}];

but I want to convert it to an object like this:

{1234:open,1235:close, 1236: pending,1237:awaiting response}

Can this be done? all the methods I have tried keep getting only the last key value pair.

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
  • I want a key value pair of id and listtext properties only – Ajiboye Adedeji Apr 19 '18 at 07:55
  • I did that, and I am just getting the last value. e.g {4321:"some status"} – Ajiboye Adedeji Apr 19 '18 at 08:01
  • 1
    No offense, but your source data is rubbish. Please try and include useful data in your questions so people don't have to try and guess at what you needed. – Tibrogargan Apr 19 '18 at 08:01
  • Possible duplicate of [How to duplicate object properties in another object?](https://stackoverflow.com/questions/9362716/how-to-duplicate-object-properties-in-another-object) – Tibrogargan Apr 19 '18 at 08:02
  • Possible duplicate of [How do I convert array of Objects into one Object in JavaScript?](https://stackoverflow.com/questions/19874555/how-do-i-convert-array-of-objects-into-one-object-in-javascript) – Herohtar May 09 '19 at 03:19

4 Answers4

8

If you are using ES6 or above:

const converted = Object.assign({}, ...myArray.map(object => ({[object.id]: object})))
dentemm
  • 6,231
  • 3
  • 31
  • 43
  • Thanks. My Initial solution was actually working. I just realized the ids are the same in my source data thats why I was getting only one key value pair. – Ajiboye Adedeji Apr 19 '18 at 08:12
  • what is this construct: `...myArray(object`? It looks like you're using an array variable as if it's a function. Is it some usage of spread or is there maybe a missing `.map`? – Tibrogargan Apr 19 '18 at 08:29
  • It is indeed the spread operator, used here to access the items in the array. Disclaimer: I did not come up with this myself ;-) Found this online somewhere and added it to my toolbelt. I find it to be a very useful oneliner – dentemm Apr 19 '18 at 08:32
  • No, not the spread. this part: `myArray(object`, Executing it gives me "myArray is not a function" – Tibrogargan Apr 19 '18 at 08:36
  • Ah now I see the problem, you are right, I am indeed missing the map() function ;-) Thanks! Funny how this got accepted without anyone noticing the bug earlier ... – dentemm Apr 19 '18 at 08:44
5

You can simply loop over the array and create a new object

var myarray=[{"id":1234, "listtext":'open', "prop3":'value3' ,"prop4": 'value4'},
             {"id":1235, "listtext":'open', "prop3":'value3' ,"prop4": 'value4'},
             {"id":1236, "listtext":'open', "prop3":'value3' ,"prop4": 'value4'},
             {"id":1237, "listtext":'open', "prop3":'value3' ,"prop4": 'value4'}];
const res = {};
myarray.forEach(obj => {
    res[obj.id] = obj.listtext;
})
console.log(res)
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
1

var myarray = [
  { id: 1234, listtext: 'open' },
  { id: 1235, listtext: 'closed' },
  { id: 1236, listtext: 'pending' },
  { id: 1237, listtext: 'open' }
]

var output = myarray.reduce(function (acc, item) {
  acc[item.id] = item.listtext
  return acc
}, {})

console.log(output)
Rich Churcher
  • 7,361
  • 3
  • 37
  • 60
  • Thanks. My Initial solution was actually working. I just realized the ids are the same in my source data thats why I was getting only one key value pair. – Ajiboye Adedeji Apr 19 '18 at 08:12
0

var myarray=[{"id":1234, "listtext":"open", "prop3":'value3' ,"prop4": 'value4'},
             {"id":1235, "listtext":"closed", "prop3":'value3' ,"prop4": 'value4'},
             {"id":1236, "listtext":"pending", "prop3":'value3' ,"prop4": 'value4'},
             {"id":1237, "listtext":"open", "prop3":'value3' ,"prop4": 'value4'}];

var myObjects = [];  // create an empty array to hold the converted objects.


// loop through the array to convert them one by one
myarray.forEach(function(element) {
  var obj = {};    // create an empty object
  obj[element.id] = element.listtext;
  myObjects.push(obj);  // add the object to the array
});


myObjects.forEach(function(object){
   console.log(object);
});
Ahmad
  • 12,336
  • 6
  • 48
  • 88