0

I have object of array of object

{
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}

I want to convert it to array of object:

[
  {"Full Name":"Phalla Morn"}, 
  {"Full Name":"Pum Chhenghorng"},
  {"Full Name":"Proen Pich"}
]

Please help.

3
  • 1
    What code have you tried? Can you show that so we can help? Commented Apr 26, 2018 at 6:21
  • 1
    Have you tried doing anything on this? The solution can't be any simpler. Commented Apr 26, 2018 at 6:21
  • I have tried many other things because I don't know how to use [].concat(...Object.values(arr)). This one works for me. thank.s Commented Apr 26, 2018 at 6:30

6 Answers 6

7

Get the internal arrays with Object.values(), and flatten by spreading into Array.concat():

const arr = {"94":[{"Full Name":"Phalla Morn"}],"95":[{"Full Name":"Pum Chhenghorng"}],"99":[{"Full Name":"Proen Pich"}]};
    
const result = [].concat(...Object.values(arr));

console.log(result);

Sign up to request clarification or add additional context in comments.

3 Comments

Except .values is not supported by IE
We live in the world of Babel and polyfills, and most importantly projects that don't need to support IE.
Lucky you. No IE support. Anyway, I found this more elegant: var arr = Object.values(obj).map(o => o[0]); Still voted up
2

Use Object.values to extract the values from an object, and then use [].concat(... to flatmap:

const input = {
  "94": [{
    "Full Name": "Phalla Morn"
  }],
  "95": [{
    "Full Name": "Pum Chhenghorng"
  }, {
    "Full Name": "example other"
  }],
  "99": [{
    "Full Name": "Proen Pich"
  }]
};
const output = [].concat(...Object.values(input));
console.log(output);

4 Comments

Not supported by IE
Nor is const nor map nor many other features, but that's what Babel and polyfills are for - best to write clear, concise code and transpile it later.
const, let map etc is supported by IE11
Oh, huh, TIL, thanks. Somehow I had gotten it into my head that 2013-IE11 didn't support any of the ES2015 features
2

You can try the following:

var obj = {
    "94":[{"Full Name":"Phalla Morn"}],
    "95":[{"Full Name":"Pum Chhenghorng"}],
    "99":[{"Full Name":"Proen Pich"}]
    }

var arr = Object.values(obj).map(o => o[0]);
console.log(arr)

4 Comments

Very elegant. No IE support without a shim
@mplungjan May be OP is working on server side and browser is not an issue. :)
true. We should ask
This won't work if there is more than one object in the array.
0

You can use two forEach loops and make sure that if there are multiple objects in array 94 or 95 then that is also taken into account by this dynamic code logic:

let obj = {
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}; 
let res = [];
Object.keys(obj).forEach((key)=>{
   obj[key].forEach((innerObj)=>{
     res.push(innerObj);
   }); 
});
console.log(res);

Or you can use Object.values together with the spread operator:

let obj = {
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}; 
let res = [];
Object.keys(obj).forEach((key)=>{
    res.push(...Object.values(obj[key]));
});
console.log(res);

1 Comment

.values is not supported by IE
0

Plain JS - since Object.values is not supported by IE at all

var arr = [],
    obj =  {"94":[{"Full Name":"Phalla Morn"}],"95":[{"Full Name":"Pum Chhenghorng"}],"99":[{"Full Name":"Proen Pich"}]};

Object.keys(obj).forEach(function(k) {
  arr.push(obj[k][0])
});
console.log(arr)

2 Comments

What if obj[k] has more that one object inside it? We can make the code run dynamically to handle that case too.
Sure. but not part of the spec
0

Try this: it should take care of arrays with multiple objects in its value.

let data = {
      "94": [{
        "Full Name": "Phalla Morn"
      }],
      "95": [{
        "Full Name": "Pum Chhenghorng"
      },{
        "Full Name": "Ryann"
      }],
      "99": [{
        "Full Name": "Proen Pich"
      }]
    };
    
    let obj = [];
    
    Object.keys(data).map(item => {
      data[item].map(item => {
        obj.push(item);
      });
    });
    
    console.log(obj);

Comments

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.