I need to convert this props:

into this array:
this.setState({
locations: [
{ label: 'California', value: 'california' },
{ label: 'Nevada', value: 'nevada' },
]
});
originally i was using this plugin and wanted to replace its default values with my props but cant convert it.
Depending on your flavor of JavaScript and preference of writing
Taking advantage of the for .. in operator, which comes with draw backs.
It'll loop over all enumerable properties even the ones from the prototype.
var arr = [];
for (var key in myObject) {
arr.push(myObject[key]);
}
or by using the Object.keys method
var arr2 = Object.keys(myObject).map(function (i) {
return myObject[i];
});
or lastly, if you're running an Babel transpiled app, Object.values
var arr3 = Object.values(myObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With