+ 1
How can I put an object in an array
I tried it like this: var modul=new Array(modul("Modul 1"), modul("Modul 2"), modul("Modul 3")); function modul(modul){ this.modul=modul; } but i cant call it with for example : alert(modul[1].modul);
36 Respostas
0
great thanks a lot it was the alert name:)
+ 2
you can do the same thing like this:
let arr = new Array();
let obj = {};
arr.push(obj);
but why if you can just write [] ?
+ 2
if anyone is interessted in the Solution of this Problem here it is:
https://code.sololearn.com/WB9udEo6YtW3
Great THANKS to NIk01
+ 1
does anyone know that?
+ 1
let arr = [];
let obj = {};
arr.push(obj)
+ 1
let is the new syntax of es6 , the same as var, but local scope.
+ 1
never )))
+ 1
i can write it by 100 ways, but itās best practice to use prototype functions, cuz they are optimized, look at array.prototype functions :)
+ 1
ok I tried it it still not working:
https://code.sololearn.com/WB9udEo6YtW3/?ref=app
+ 1
function modul(name, subject) {
return {
name: name,
subject: subject
}
}
+ 1
look,if you write, this.name inside of a function, itās just creating a variable inside that function, thats all, you need to return something from there, to be able to push it into array.
+ 1
can't report quangtong
0
for what is the let expression?
0
got it it's for the scope
0
I'll try it
0
when do I use new array?
0
cause it was in the js lections
0
I'm wrong it was just with objects got confused :p Thanks a lot
0
and i have to write it with the push