How do I make sure that the json variable is always an array? In my code, sometimes str is an array and sometimes it isn't. Is there a way to make sure that it's always an array?
// array
const str = '[{ "key": "value" }]';
const json = JSON.parse(str);
console.log(json);
// [{
// key: 'value'
// }]
// json
const str = '{ "key": "value" }';
const json = JSON.parse(str);
console.log(json);
// {
// key: 'value'
// }
// What I want to happen somehow
const str = '{ "key": "value" }';
const json = JSON.parse(str);
console.log(json);
// [{
// key: 'value'
// }]
I could check the type of json using instanceof, but I was hoping there would be a fancy ES6 way (with spread?) happen without it giving me an array of arrays with str is wrapped in brackets.