I need to create an array of object literals like this:
var myColumnDefs = [
{key:\"label\", sortable:true, resizeable:true},
{key:\"notes\", sortabl
In the same idea of Nick Riggs but I create a constructor, and a push a new object in the array by using it. It avoid the repetition of the keys of the class:
var arr = [];
var columnDefs = function(key, sortable, resizeable){
this.key = key;
this.sortable = sortable;
this.resizeable = resizeable;
};
for (var i = 0; i < len; i++) {
arr.push((new columnDefs(oFullResponse.results[i].label,true,true)));
}
This is what Array#map are good at
var arr = oFullResponse.results.map(obj => ({
key: obj.label,
sortable: true,
resizeable: true
}))
If you want to go even further than @tetra with ES6 you can use the Object spread syntax and do something like this:
let john = {
firstName: "John",
lastName: "Doe",
};
let people = new Array(10).fill().map((e, i) => {(...john, id: i});