1
votes

plusieurs objets ne sont pas poussés dans un tableau et ne créent pas de tableau d'objets

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"},......]

0 commentaires

3 Réponses :



1
votes

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)


0 commentaires

0
votes
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));

0 commentaires