How can I put an object in an array | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 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);

31st Oct 2018, 12:47 PM
jack
jack - avatar
36 Respuestas
0
great thanks a lot it was the alert name:)
31st Oct 2018, 3:42 PM
jack
jack - avatar
+ 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 [] ?
31st Oct 2018, 2:53 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 2
if anyone is interessted in the Solution of this Problem here it is: https://code.sololearn.com/WB9udEo6YtW3 Great THANKS to NIk01
31st Oct 2018, 3:58 PM
jack
jack - avatar
+ 1
does anyone know that?
31st Oct 2018, 2:06 PM
jack
jack - avatar
+ 1
let arr = []; let obj = {}; arr.push(obj)
31st Oct 2018, 2:39 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
let is the new syntax of es6 , the same as var, but local scope.
31st Oct 2018, 2:43 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
never )))
31st Oct 2018, 2:49 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 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 :)
31st Oct 2018, 2:55 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
ok I tried it it still not working: https://code.sololearn.com/WB9udEo6YtW3/?ref=app
31st Oct 2018, 3:02 PM
jack
jack - avatar
+ 1
function modul(name, subject) { return { name: name, subject: subject } }
31st Oct 2018, 3:05 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 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.
31st Oct 2018, 3:10 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
31st Oct 2018, 3:20 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
can't report quangtong
2nd Nov 2018, 5:59 PM
jack
jack - avatar
0
for what is the let expression?
31st Oct 2018, 2:40 PM
jack
jack - avatar
0
got it it's for the scope
31st Oct 2018, 2:43 PM
jack
jack - avatar
0
I'll try it
31st Oct 2018, 2:45 PM
jack
jack - avatar
0
when do I use new array?
31st Oct 2018, 2:48 PM
jack
jack - avatar
0
cause it was in the js lections
31st Oct 2018, 2:50 PM
jack
jack - avatar
0
I'm wrong it was just with objects got confused :p Thanks a lot
31st Oct 2018, 2:53 PM
jack
jack - avatar
0
and i have to write it with the push
31st Oct 2018, 2:54 PM
jack
jack - avatar