Javascript Array Push and Display

Viewed 756

I am doing a Laravel project with editable inline with select option however I want I manage to query the brands and I want to display the array after I push it in the source. Please help

var brand = [];
data_brand.forEach(function(element) {   
    var branddetails = {value: element.id, text: element.brand_name}; 
    brand.push(branddetails);
});
$(function(){
    $('#brand').editable({
        value: 2,    
        source: [

            //I want to output like this {value: 1, text: 'Active'},
            brand.values() // this code does not work
        ]
    });
});
3 Answers

This should work:

source: brand.map(item => item)

or simply:

source: brand

In order to display the array elements, use loop.

Example-

let branddetails = [{value: 1, text: "something" }];

branddetails.forEach(brand => console.log(brand));

-- Edit --

Instead of creating array and then getting the pushed element, you can directly add the element itself in the source array.

let branddetails;
data_brand.forEach(function (element) {
  branddetails = { value: element.id, text: element.brand_name };
});
$(function () {
  $('#brand').editable({
    value: 2,
    source: [
      branddetails 
    ]
  });
});

brand is an array and you since source also expecting the array, You can try like this

source: [...brand]

Related