I used a literal as a dictionary, but a third party binding tool only takes arrays.
This is one way, is there a better one?
var arr = [];
$.each(objectLiteral, function () { arr.push(this); });
To convert an object to an array you use one of three methods: Object. keys() , Object. values() , and Object. entries() .
Using numpy.asarray() , and true (by default) in the case of np. array() . This means that np. array() will make a copy of the object (by default) and convert that to an array, while np.
Object. entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter.
const objectLiteral = { hell: 'devil' };
const ver1 = Object.keys(objectLiteral); // ['hell']
const ver2 = Object.values(objectLiteral); // ['devil']
const ver3 = Object.entries(objectLiteral); // [['hell', 'devil']]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/entries
I think there is nothing wrong with your solution.
This is a shorter one:
var arr = $.map(objectLiteral, function (value) { return value; });
Your method is fine, clear and readable. To do it without jQuery, use the for (..in..)
syntax:
var arr = [];
for (prop in objectLiteral) {
arr.push(objectLiteral[prop]);
}
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