-5

I have a object like this

users = {
         user1:{name: bob,
               age:23},
         user2:{name:rob,
                age:24},
         user3:{name:jay,
                age:30}}

How to convert this object into an array like [user1:{name: bob,age:23},user2{name:rob,age:24},user3:{name:jay, age:30}]

  • 2
    It's unclear what you're asking here. Arrays in JavaScript can have properties, but they won't be elements in the array. Arrays are inherently integer-indexed. What is the problem you're trying to solve? There's likely a better way to accomplish what you're trying to do. – Marsroverr Jul 20 '20 at 18:07
  • If your end goal is being able to iterate over the object, see answers on [this question](https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object) – Marsroverr Jul 20 '20 at 18:08
  • I have an object which contains different objects, I need to convert that object into an array of objects – Anush Lunsavath Jul 20 '20 at 18:11
  • But why do you need an array? Why not keep it as an object? – Marsroverr Jul 20 '20 at 18:13
  • In terms of actually converting it to an array, put the keys in the object and append them to the array, making the array look like this: `[ { id: user1, name: bob, age:23}, {...}, ...]` – Marsroverr Jul 20 '20 at 18:14
  • Im getting the object through JSON. In order to use it, I need to convert it into an array. – Anush Lunsavath Jul 20 '20 at 18:18
  • Why do you need to convert it to an array to use it, and are you sure you can't accomplish this use case with an object? – Marsroverr Jul 20 '20 at 18:21
  • For converting the object to an array, James' answer below does exactly what you're asking. – Marsroverr Jul 20 '20 at 18:22
  • 1
    @MLarionov : that is actually kind of funny looking at your attempt to convince OP in what (***from your point of view***) is better for them. As for me, I voted to close as OP didn't even bother to provide valid expected output, not to mention about no own attempt to resolve the issue. – Yevhen Horbunkov Jul 20 '20 at 20:55

2 Answers2

1

Simple, map the Object.keys array of your object to the array you want. I added in the object key as the "id" field.

const users = {
         user1:{name: 'bob',
               age:23},
         user2:{name:'rob',
                age:24},
         user3:{name:'jay',
                age:30}};
                
const arr = Object.keys(users).map(key => ({id: key, ...users[key]}));

console.log(arr);
James
  • 20,957
  • 5
  • 26
  • 41
0

the map solution is more up to date but sometimes old vanilla js is easier to understand

const users =  {user1:{name:'bob',age:23},user2:{name:'rob',age:24},user3:{name:'jay',age:30}};
                
    var myArray=[];
    
    for(let i =1;i<4;i++){       
       var user={['user'+i]:users['user'+i]}  //create your object
       myArray.push(user);                    //add it to array
    }
     
console.log(myArray[0]['user1']) // remember arrays are indexed []
console.log(myArray[1]['user2'])
console.log(myArray[2]['user3'])

console.log(myArray);
.as-console-wrapper{min-height:100%;})
DCR
  • 14,737
  • 12
  • 52
  • 115