0

I have an example of javascript array of object within an object there is object. So I basically want only that into one dimensional array.

I have searched lots of Q's on SO but none of them as per my requirement.

What I have

[
{
"id": "59cf758f7bdf8d2e0c1c68c7",
"value": {
  "city": "Mahbubnagar",
  "state": "Andhra Pradesh",
  "country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c8",
"value": {
  "city": "Udgir",
  "state": "Maharashtra",
  "country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68c9",
"value": {
  "city": "Umarga",
  "state": "Maharashtra",
  "country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68ca",
"value": {
  "city": "Umarkhed",
  "state": "Maharashtra",
  "country": "India"
}
},
{
"id": "59cf758f7bdf8d2e0c1c68cb",
"value": {
  "city": "Umred",
  "state": "Maharashtra",
  "country": "India"
}
}
]

And I simply want this array of objects into an array

[
  "59cf758f7bdf8d2e0c1c68c7",
  "Mahbubnagar",
  "Andhra Pradesh",
  "India",
],[
  "59cf758f7bdf8d2e0c1c68c9",
  "Umarga",
  "Maharashtra",
  "India",
],[
  "59cf758f7bdf8d2e0c1c68ca",
  "Umarkhed",
  "Maharashtra",
  "India",
],[
  "59cf758f7bdf8d2e0c1c68c7",
  "Mahbubnagar",
  "Andhra Pradesh",
  "India",
],

The array of objects could be have more children in other words you can say its dynamic content.

What I have used so far is

var object= property.value.elements;
        var finalArray = object.map(function (obj) {
          return obj.id;
        });
        console.log(finalArray);

Which is giving me only list of Id's

It's not duplicate question please verify before marking it as Duplicate.

8
  • Your example output is not syntactically correct. Commented May 18, 2018 at 11:55
  • How its not correct ? @ninesalt Commented May 18, 2018 at 11:56
  • I have added what I have tried @NinaScholz Commented May 18, 2018 at 11:56
  • Copy/paste your output again...what you've posted is incorrect Commented May 18, 2018 at 11:57
  • Your output is just a series of arrays not wrapped in an array. You probably mean to wrap the strings in an object [{}, {}...] Commented May 18, 2018 at 11:57

3 Answers 3

1

You could check if a value of the object is an object an take the flattened valued for the result.

function flat(object) {
    return Object
        .values(object)
        .reduce((r, v) => r.concat(v && typeof v === 'object' ? flat(v) : v), []);
}

var array = [{ id: "59cf758f7bdf8d2e0c1c68c7", value: { city: "Mahbubnagar", state: "Andhra Pradesh", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c8", value: { city: "Udgir", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68c9", value: { city: "Umarga", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68ca", value: { city: "Umarkhed", state: "Maharashtra", country: "India" } }, { id: "59cf758f7bdf8d2e0c1c68cb", value: { city: "Umred", state: "Maharashtra", country: "India" } }],
    result = array.map(flat);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

Try following

var arr = [{"id":"59cf758f7bdf8d2e0c1c68c7","value":{"city":"Mahbubnagar","state":"Andhra Pradesh","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c8","value":{"city":"Udgir","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68c9","value":{"city":"Umarga","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68ca","value":{"city":"Umarkhed","state":"Maharashtra","country":"India"}},{"id":"59cf758f7bdf8d2e0c1c68cb","value":{"city":"Umred","state":"Maharashtra","country":"India"}}];

function getVals(obj, arr) {
  Object.entries(obj).forEach(([key, value]) => {
    if(typeof value === "object") {
      arr = [...arr, ...getVals(obj[key], arr)];
    } else {
      arr.push(value);
    }
  });
  return arr;
}

var resp = arr.map((item) => {
  let temp = [];
  getVals(item, temp);
  return temp;
});
console.log(resp);

2 Comments

Does this work on many levels of objects inside array or just two level @nikhil
Thanks a lot @nikhil
0

This isnt the easiest way to do it. Just loop over all the objects and extract their values.

let x = [{"id": "59cf758f7bdf8d2e0c1c68c7", "value": {...}, ..] 
let res = []

x.forEach((obj, index) => {
  res.push([obj.id, obj.value.city, obj.value.state, obj.value.country])
}) 

console.log(res)

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.