GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing uz.javascript.info — Obyektlarga xarita

  • Qo'llanma
  • JavaScript dasturlash tili
  • Ma'lumot turlari
  • Massiv usullari
darsga qaytish

Obyektlarga xarita

Sizda user obyekyektlar massivi mavjud, ularning har birida ism, familiya va id mavjud.

Undan id va fullName obyektlarining boshqa massivni yaratish uchun kodni yozing, bu yerda to'liq ism ism va familiya dan hosil bo’ladi.

Masalan:

let john = { name: "John", surname: "Smith", id: 1 };
let pete = { name: "Pete", surname: "Hunt", id: 2 };
let mary = { name: "Mary", surname: "Key", id: 3 };

let users = [ john, pete, mary ];

let usersMapped = /* ... sizning kodingiz ... */

/*
usersMapped = [
  { fullName: "John Smith", id: 1 },
  { fullName: "Pete Hunt", id: 2 },
  { fullName: "Mary Key", id: 3 }
]
*/

alert( usersMapped[0].id ) // 1
alert( usersMapped[0].fullName ) // John Smith

Shunday qilib, aslida siz bir obyektlar massivini boshqasiga solishtirishingiz kerak. Bu erda => dan foydalanib ko’ring.

let john = { name: "John", surname: "Smith", id: 1 };
let pete = { name: "Pete", surname: "Hunt", id: 2 };
let mary = { name: "Mary", surname: "Key", id: 3 };

let users = [ john, pete, mary ];

let usersMapped = users.map(user => ({
  fullName: `${user.name} ${user.surname}`,
  id: user.id
}));

/*
usersMapped = [
  { fullName: "John Smith", id: 1 },
  { fullName: "Pete Hunt", id: 2 },
  { fullName: "Mary Key", id: 3 }
]
*/

alert( usersMapped[0].id ); // 1
alert( usersMapped[0].fullName ); // John Smith

Iltimos, o’q funktsiyalari uchun biz qo’shimcha qavslardan foydalanishimiz kerakligini unutmang.

Biz bunday yozolmaymiz:

let usersMapped = users.map(user => {
  fullName: `${user.name} ${user.surname}`,
  id: user.id
});

Yodimizda bo’lganidek, ikkita o’q funktsiyasi mavjud: tanasiz value => expr va tanali value => {...}.

Bu erda JavaScript { ni obyektning boshlanishi emas, balki funktsiya tanasining boshlanishi sifatida qabul qiladi. Vaqtinchalik yechim ularni “oddiy” qavslarga o’rashdan iborat:

let usersMapped = users.map(user => ({
  fullName: `${user.name} ${user.surname}`,
  id: user.id
}));

Endi yaxshi.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.