-2

I have a javascript object that I would like to convert into an array of objects

 {
        "mongo": {
            "status": true,
            "err": ""
        },
        "redis": {
            "status": true,
            "err": ""
        },
        "rabbitmq": {
            "status": true,
            "err": ''
        }
}

The expected output must be

 [
        "mongo": {
            "status": true,
            "err": ""
        },
        "redis": {
            "status": true,
            "err": ""
        },
        "rabbitmq": {
            "status": true,
            "err": ""
        }
]

What is the correct way to achieve this with javascript code?

Thanks.

fgamess
  • 1,276
  • 2
  • 13
  • 25
Tej Kumar
  • 21
  • 1
  • 2
  • 1
    Please visit the [help center](https://stackoverflow.com/help), take the [tour](https://stackoverflow.com/tour) to see what and [How to Ask](https://stackoverflow.com/help/how-to-ask). Do some research, search for related topics on SO; if you get stuck, post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your attempt, noting input and expected output. – Narendra Jadhav Aug 28 '18 at 08:31
  • I can't see what will change from that – jonatjano Aug 28 '18 at 08:32
  • 1
    Your expected output is incorrect – Ankit Agarwal Aug 28 '18 at 08:33

3 Answers3

2

Your expected output is not syntactically correct in javascript. JS arrays can have only numeric indices starting from 0. In you expected output, you have shown string keys.

The syntactically and symantically correct output would be:

[
    {
        "name": "mongo",
        "status": true,
        "err": ""
    },
    {
        "name": "redis",
        "status": true,
        "err": ""
    },
    {
        "name": "rabbitmq",
        "status": true,
        "err": ""
    }
]

JS Code to achieve this:

var obj = {
    "mongo": {
        "status": true,
        "err": ""
    },
    "redis": {
        "status": true,
        "err": ""
    },
    "rabbitmq": {
        "status": true,
        "err": ''
    }
};
var arr = [];
for (key in obj) {    
    arr.push(Object.assign(obj[key], {name: key}));
}
console.log('sdf', arr);
Ketan Yekale
  • 2,108
  • 3
  • 26
  • 33
0
"mongo": {
  "status": true,
  "err": ""
}

is not really an object or valid syntax in that matter. You can have

[
  {
    "status": true,
    "err": ""
  },
  {
    "status": true,
    "err": ""
  },
  {
    "status": true,
    "err": ""
  }
]

from this

Object.keys(obj).reduce((arr, key)=>arr.concat(obj[key]), []);

assuming obj is your object and your free to use ES6 syntax. Or you can have this:

[
  {
    "mongo": {
      "status": true,
      "err": ""
    }
  },
  {
    "redis": {
      "status": true,
      "err": ""
    }
  },
  {
    "rabbitmq": {
      "status": true,
      "err": ""
    }
  }
]

from this:

Object.keys(obj).reduce((arr, key)=>{
    const subObj = {[key]: obj[key]};
    return arr.concat(subObj)
}, []);
-1

incorrect expected output, as you mentionned, it's not an array it can be as follow :

[
        {
            "name": "mongo",
            "status": true,
            "err": ""
        },
        {
            "name" : "redis",
            "status": true,
            "err": ""
        },
        {
            "name" : "rabbitmq",
            "status": true,
            "err": ""
        }
]

and to obtain such an output, you must write a custom function for that.