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.

5
  • I want a key value pair of id and listtext properties only Commented Apr 19, 2018 at 7:55
  • I did that, and I am just getting the last value. e.g {4321:"some status"} Commented Apr 19, 2018 at 8: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. Commented Apr 19, 2018 at 8:01
  • Possible duplicate of How to duplicate object properties in another object? Commented Apr 19, 2018 at 8:02
  • Possible duplicate of How do I convert array of Objects into one Object in JavaScript? Commented May 9, 2019 at 3:19

4 Answers 4

8

If you are using ES6 or above:

const converted = Object.assign({}, ...myArray.map(object => ({[object.id]: object})))
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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?
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
No, not the spread. this part: myArray(object, Executing it gives me "myArray is not a function"
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 ...
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)

4 Comments

You have made a small mistake: you are using a const when you initiate the empty object
That's not really a mistake, though it might be confusing if ES6 is not being used. Assigning properties to a const object will work just fine.
@dentemm, since we are not reassigning res, we can declare it as a const obj
Ah yes indeed, my mistake!
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)

1 Comment

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

3 Comments

How does this improve on @ShubhamKhatri's answer?
@Tibrogargan When I posted my answer, I was not aware someone else wrote the same exact answer
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.

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.