You can simply use the $.map() method to convert a JavaScript object to an array of items.
The $.map() method applies a function to each item in an array or object and maps the results into a new array. Let's take a look at an example to understand how it basically works:
<script>
var myObj = {
name: "Peter",
age: 28,
gender: "Male",
email: "peterparker@mail.com"
};
// Converting JS object to an array
var array = $.map(myObj, function(value, index){
return [value];
});
console.log(array);
// Prints: ["Peter", 28, "Male", "peterparker@mail.com"]
</script>
Let's check out one more example where an object is converted into an array of array:
Use the jQuery
$.map()MethodYou can simply use the
$.map()method to convert a JavaScript object to an array of items.The
$.map()method applies a function to each item in an array or object and maps the results into a new array. Let's take a look at an example to understand how it basically works:Let's check out one more example where an object is converted into an array of array:
need an explanation for this answer? contact us directly to get an explanation for this answer