Im trying something which is probably very easy but i cant seem to find out why its not working. Im trying to dynamically create and array with jquery/javascript.
my code;
var icons = $('.icon');
var desktopicons = [];
var user = { name: username };
var iconsetup = { myicons: [] };
desktopicons.push(user);
desktopicons.push(iconsetup);
$.each(icons, function() {
var name = $(this).attr('name');
var rel = $(this).attr('rel');
var icon = {
icon: [{
name: name,
rel: rel
}]
};
iconsetup.myicons[0].push(icon);
});
desktopicons.push(user);
desktopicons.push(iconsetup);
$('#desktop').append(desktopicons[0].name);
$('#desktop').append(desktopicons[1].myicons[0].icon[0].name);
Somehow my log file says cannot call method push of undefined on 'iconsetup.myicons[0].push(icon);' this line.
Anyone who can tell me how to create the array? Thanks!
iconsetup.myicons
is an empty array.[0]
,iconsetup.myicons.push(icon);