var myArray = ["id", "name", "location", "email", "gender", "tel / fax", "language preference", "credit card type"]
for (var key in myArray) {
this.col.data = myArray[key];
this.col.type = "text";
console.log('col=>' + JSON.stringify(this.col))
this.dynamicColumns.push(this.col);
console.log('dynamicColumns=>' +
JSON.stringify(this.dynamicColumns))
}
but i want this output:- [{"data":"id","type":"text"},{"data":"name","type":"text"},{"data":"location","type":"text"},......]
3 Réponses :
Vous pouvez simplement utiliser map et créez un objet au format souhaité pour chaque élément.
var myArray = ["id", "name", "location", "email", "gender", "tel / fax", "language preference", "credit card type"]
let op = myArray.map(ele =>({ data: ele, type: 'text' }))
console.log(op)
var myArray = ["id", "name", "location", "email", "gender", "tel / fax", "language preference", "credit card type"];
var dynamicColumns = [];
for (var key in myArray) {
this.col = {};
this.col.data = myArray[key];
this.col.type = "text";
console.log('col=>' + JSON.stringify(this.col))
dynamicColumns.push(this.col);
}
console.log('dynamicColumns=>' +
JSON.stringify(dynamicColumns));