Hai sa vorbim despre UnderscoreJS: Partea II

postat acum 3 ani de Stefanescu Mihai in categorie iNoob

In primul articol despre UnderscoreJS v-am vorbit despre ce este aceasta librarie si ce puteti face cu ea. In acest articol am sa va vorbesc despre functia each ce ne este pusa la dispozitie de librarie.

_.each: Comenzi repetitive usor de inteles

Nu cred ca exista proiect in care sa nu ai nevoie de cel putin o bucatica de cod care sa arate catusi de putin cu urmatoarea:

var mancare = ['Cotlet', 'Piept de pui', 'Clatite'];

for(var i = 0; i < mancare.length; i++) {
  console.log('mancare: ' + mancare[i]);
}

UnderscoreJS ne permite sa scriem acest lucru mult mai simplu:

var mancaruri = ['Cotlet', 'Piept de pui', 'Clatite'];

_.each(mancaruri, function(mancare, index, mancaruri) {
  console.log('mancare: ' + mancare);
});

Tare, nu? Functia  _.each() primeste 2 parametri:

  • Un array sau un obiect prin care sa itereze
  • O functie callback

Pentru fiecare element din array, _.each() va apela functia callback (in documentatie se gaseste ca si iteratee). In aceasta functie callback avem acces la 3 parametri:

  • Valoarea arrayului la iteratia curenta (mancare).
  • Numarul iteratiei curente (index).
  • Array-ul prin care iteram (mancaruri).

Dupa cum puteti observa codul este mult mai lizibil si putem accesa elementele fara sa fim nevoiti sa scriem mancaruri[i], cum am fi folosit intr-un  for.

In urmatorul articol din aceasta serie am sa va vorbesc despre modul in care putem folosi UnderscoreJS ca si template engine.

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group

Comentariu postate de gold ira approved la data de 16.07.2017
Hey there! Do you use Twitter? I'd like to follow you if that would be okay. I'm absolutely enjoying your blog and look forward to new updates.
Comentariu postate de good gold coin investments la data de 17.07.2017
This site was... how do you say it? Relevant!! Finally I have found something which helped me. Many thanks!
Comentariu postate de assistir filmes online gratis hd la data de 17.07.2017
Na Wood's Natal Bom João já começou. https://www.michaelfrancislidman.com/wiki/index.php?title=Filmes_Recomendados_Ao_Oscar_Tamb%C3%83_m_Est%C3%83%C2%A3o_No_Alarde_Nos_Cinemas
Comentariu postate de https://demos3.softaculous.com la data de 17.07.2017
O qᥙe se passa companheiros , fastidioso post e agradável exortando comentou neste lugar, estou realmente desfrutando ⲣоr еstas. https://demos3.softaculous.com/RicarGBooK/
Comentariu postate de nhà cái w88 la data de 21.07.2017
This site was... how do you say it? Relevant!! Finally I have found something which helped me. Many thanks!
Comentariu postate de chrome hearts outlet la data de 13.05.2019
I wanted to put you the tiny note just to say thanks as before for your personal amazing thoughts you've provided above. It has been unbelievably open-handed with you to make extensively precisely what some people would have advertised for an electronic book to generate some money for their own end, chiefly seeing that you could possibly have tried it if you desired. Those good ideas in addition served to become easy way to know that most people have similar eagerness similar to my personal own to know lots more in respect of this problem. I am certain there are numerous more pleasant times up front for individuals that scan through your blog post.
Comentariu postate de yeezy boost 350 la data de 18.05.2019
Thanks so much for giving everyone an extraordinarily pleasant chance to check tips from this website. It is often so superb plus packed with a good time for me and my office mates to search your web site at least thrice per week to see the latest tips you have. And indeed, I am just actually fulfilled with your striking principles served by you. Selected 1 facts in this posting are rather the most efficient I've ever had.