“Tushar Jadhav” Respostas de código

Tushar Jadhav

for(var i = 0; i < fruits.length; i++) {
    var obj = {}; // <---- Move declaration inside loop

    obj['data'] = fruits[i];
    obj['label'] = label;
    arr.push(obj);
}
Successful Seal

Tushar Jadhav

var arr = [];
var fruits = ['banana', 'apple', 'mango'];
var label = 'Fruits';

for (var i = 0; i < fruits.length; i++) {
    var obj = {};
    obj['data'] = fruits[i];
    obj['label'] = label;
    arr.push(obj);
}
Successful Seal

Tushar Jadhav

var arr = fruits.map(fruit => ({
    data: fruit,
    label: label
}));
Successful Seal

Procure respostas de código populares por idioma

Procurar outros idiomas de código