How to fill array with a for cycle

3

I'm using a library of jquery DDslick to fill the input selects with styles even images, the library uses arrays to fill them for example ..

var data= 
      [
       { text: "Facebook",//este es el texto que se ve en la cabecera 
         value: 1,//este es el valor de el select,puedes poner el que quieras
         selected: false//opccion si deceas que este seleccionado o no
       }
      ]

this div would be the select emulator

<div id="Dropdown"></div><!--este seria el emulador del select-->

and that would be how it would fill that div to be a select

$('#Dropdown').ddslick({
    data:data,//esta es la variable data que llenara el div
    width:300,//manipular estilos del select 
    selectText: "Select your preferred social network",
    imagePosition:"right",
    onSelected: function(selectedData){//este metodo serai el onchange
        //callback function: do something with selectedData;
    }   
});

this is the Documentation of the library if you are interested

Now, what I want to do is fill an array with dates of a for , so write dates from 1970 to 2020 one by one in a array would not be very funny of say XD, so I need the array to have the same structure that the library uses so I can accept it hopefully understand me, in this snipper you will see the selects with styles p>

//Dropdown plugin data
var ddData = [{text:"any",value:"any"}];
// Si el objeto es igual puedes hacer algo así. 
for(var i=1970; i<2020; i++){
   // Tu fecha
   var dataFecha=i;
   // Creas un nuevo objeto.
   var objeto =   {
            // Le agregas la fecha
            fecha: dataFecha,
            text: i,
            value: i,
            selected: false
   }
 
  ddData.push(objeto);
} 

$('#Dropdown').ddslick({
    data:ddData,
    width:300,
     height:300,
    selectText: "vehicle year",
    imagePosition:"right",
    onSelected: function(data){
    
      var d=data.selectedData.value;
      $("#show").html(d)
    }   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<script type="text/javascript" src="https://cdn.rawgit.com/prashantchaudhary/ddslick/master/jquery.ddslick.min.js" ></script>


<div id="Dropdown"></div>

<div id="show"></div>


<div id="fecha"></div><!--need to fill out this one width date-->

What I would like to fill this array with dates something like this but the truth is that I do not know how to do it well so I resorted to you, I hope you can help me thanks!

for(var i=1970; i<2020; i++){

   var dataFecha=i;
}
    
asked by andy gibbs 04.10.2018 в 15:27
source

1 answer

3
// Si el objeto es igual puedes hacer algo así. 

    var ddData = [];
    
    for(var i=1970; i<2020; i++){
       // Tu fecha
       var dataFecha=i;
       // Creas un nuevo objeto.
       var objeto =   {
                // Le agregas la fecha
                fecha: dataFecha,
                text: "Foursquare",
                value: 4,
                selected: false,
                description: "Description with Foursquare",
                imageSrc: "http://i.imgur.com/kFAk2DX.png"
       }
      //Lo agregas al array.
      ddData.push(objeto);
    } 
    
    for ( x in ddData) {
        console.log( ddData[x] );
    }

   // Si el array ya contiene datos y quieres agregar fechas  puedes 
    // hacer algo como:
    
    var ddData = [
{
    text: "Facebook",
    value: 1,
    selected: false,
    description: "Description with Facebook",
    imageSrc: "http://i.imgur.com/XkuTj3B.png"
},
{
    text: "Twitter",
    value: 2,
    selected: false,
    description: "Description with Twitter",
    imageSrc: "http://i.imgur.com/8ScLNnk.png"
},
{
    text: "LinkedIn",
    value: 3,
    selected: true,
    description: "Description with LinkedIn",
    imageSrc: "http://i.imgur.com/aDNdibj.png"
},
{
    text: "Foursquare",
    value: 4,
    selected: false,
    description: "Description with Foursquare",
    imageSrc: "http://i.imgur.com/kFAk2DX.png"
}
];
    
    var contadorFecha = 1970;
    for ( x in ddData) {
       
        
        ddData[x].fecha = contadorFecha;
        contadorFecha++;
    }
    
answered by 04.10.2018 / 15:37
source