JavaScript Arrays and Objects Practice
JavaScript Arrays మరియు Objects Practice
Arrays మరియు objects JavaScript లో data ను సులభంగా store మరియు manage చేయడానికి ఉపయోగపడతాయి. Array లో multiple values ను order లో ఉంచవచ్చు. Object లో ఒక entity కి సంబంధించిన properties మరియు values ను key-value pairs రూపంలో ఉంచవచ్చు.
Array లో objects
ఒక array లో objects ను ఉంచితే students list, products list లేదా employees list వంటి real-world data ను represent చేయవచ్చు.
ఉదాహరణకు, students[0].name ద్వారా మొదటి student యొక్క name ను access చేయవచ్చు. students[0].marks ద్వారా ఆ student యొక్క marks array ను పొందవచ్చు.
Nested data తో పని చేయడం
Object లో array ఉండవచ్చు. అలాగే array లో objects ఉండవచ్చు. ఈ combination ను nested data అంటారు. Nested data నుంచి value పొందడానికి dot notation మరియు square bracket notation ఉపయోగించవచ్చు.
ఉదాహరణకు, student.marks[1] అంటే student object లోని marks array లో రెండవ value. Array index ఎప్పుడూ zero నుంచి ప్రారంభమవుతుంది.
Practice approach
- మొదట data structure ను జాగ్రత్తగా పరిశీలించండి.
- Array లో ఎన్ని items ఉన్నాయో గుర్తించండి.
- ప్రతి object లోని property names ను గుర్తించండి.
- సరైన index మరియు property name ఉపయోగించి values ను access చేయండి.
- လိုగిన calculation లేదా update ను ప్రతి item పై apply చేయండి.
Simple Explanation
Array ను ఒక list లాగా, object ను ఒక form లేదా record లాగా ఊహించండి. ఉదాహరణకు, ఒక shopping cart అనేది products objects ఉన్న array. ప్రతి product object లో name, price మరియు quantity ఉంటాయి.
ఒక item యొక్క మొత్తం ధరను తెలుసుకోవడానికి item.price * item.quantity ఉపయోగించవచ్చు. అన్ని items యొక్క ధరలను కలపడానికి ఒక total variable ఉపయోగించి ప్రతి object ను ఒక్కొక్కటిగా process చేయాలి.
ఈ practice ద్వారా arrays లో objects ను access చేయడం, object properties చదవడం మరియు data ఆధారంగా calculation చేయడం నేర్చుకుంటారు.
Real-Life Example
ఒక online shopping cart ను ఊహించండి. Cart లో అనేక products ఉంటాయి. ప్రతి product కు name, price మరియు quantity ఉంటాయి. Cart total తెలుసుకోవడానికి ప్రతి product యొక్క price ను quantity తో multiply చేసి, అన్ని results ను కలపాలి.
ఇక్కడ మొత్తం cart ఒక array, ప్రతి product ఒక object, మరియు price तथा quantity ఆ object యొక్క properties.
Code Examples
Array లో objects ను access చేయడం
const students = [
{ name: "Ravi", score: 85 },
{ name: "Sita", score: 92 }
];
console.log(students[0].name);
console.log(students[1].score);
students ఒక array. ఇందులో రెండు student objects ఉన్నాయి. students[0].name మొదటి object లోని name ను ఇస్తుంది. students[1].score రెండవ object లోని score ను ఇస్తుంది.
Ravi 92
Object లోని array values ను ఉపయోగించడం
const student = {
name: "Anu",
marks: [80, 90, 85]
};
student.marks.push(95);
const total = student.marks[0] + student.marks[1] + student.marks[2] + student.marks[3];
console.log(student.name);
console.log(total);
console.log(total / student.marks.length);
student object లో marks అనే array ఉంది. push() ద్వారా 95 ను marks array చివర చేర్చాము. తరువాత అన్ని marks ను కలిపి total కనుగొని, marks count తో divide చేసి average కనుగొన్నాము.
Anu 350 87.5
Cart items యొక్క total price
const cart = [
{ name: "Book", price: 300, quantity: 2 },
{ name: "Pen", price: 20, quantity: 3 }
];
let total = 0;
for (const item of cart) {
total = total + item.price * item.quantity;
}
console.log(total);
cart లో ప్రతి item ఒక object. for…of loop ప్రతి item ను ఒక్కొక్కటిగా తీసుకుంటుంది. ప్రతి item కోసం price మరియు quantity ను multiply చేసి total కు add చేస్తాము.
660
Line-by-Line Explanation
ఇక్కడ student అనే constant object ను ప్రారంభిస్తున్నాము.
Object లో name property కి Anu అనే string value ఇస్తున్నాము.
marks property లో మూడు numbers ఉన్న array ను store చేస్తున్నాము.
ఇది student object declaration ను ముగిస్తుంది.
Dot notation ద్వారా marks array ను access చేసి, push() method తో 95 ను చివరలో చేర్చుతున్నాము.
marks array లోని నాలుగు values ను access చేసి వాటిని add చేసి total variable లో store చేస్తున్నాము. Array index zero నుంచి ప్రారంభమవుతుంది.
student object లోని name property value ను console లో print చేస్తున్నాము.
మొత్తం marks అయిన total value ను console లో print చేస్తున్నాము.
total ను marks array length తో divide చేసి average ను console లో print చేస్తున్నాము.
Try It
Try this JavaScript code:
const cart = [
{ name: "Notebook", price: 120, quantity: 2 },
{ name: "Pencil", price: 10, quantity: 5 },
{ name: "Bag", price: 800, quantity: 1 }
];
let total = 0;
for (const item of cart) {
// ప్రతి item యొక్క price మరియు quantity ఉపయోగించి total ను update చేయండి.
}
console.log(total);
Practice
Practice:
పైన ఉన్న cart array ను ఉపయోగించి అన్ని products యొక్క total price ను కనుగొనండి. ప్రతి item యొక్క price ను quantity తో multiply చేసి total కు add చేయాలి.
Hint:
for…of loop లో item.price * item.quantity ను calculate చేసి, total variable కు add చేయండి.
Practice:
ఒక students array లో ఉన్న రెండవ student యొక్క name మరియు score ను console లో print చేయండి.
Hint:
రెండవ item కోసం index 1 ఉపయోగించండి. తరువాత .name మరియు .score properties ను access చేయండి.
Common Mistakes
Array index 1 నుంచి ప్రారంభమవుతుందని అనుకోవడం.
JavaScript arrays zero-based. మొదటి item index 0, రెండవ item index 1.
Object property access లో property name ను తప్పుగా రాయడం, ఉదాహరణకు item.quanity.
Object లో ఉన్న exact property name ను ఉపయోగించాలి. సరైన spelling item.quantity.
Price మరియు quantity ను add చేయడం, multiply చేయకుండా total calculation చేయడం.
ఒక item యొక్క cost కోసం price * quantity ఉపయోగించాలి. తరువాత ఆ cost ను total కు add చేయాలి.
ఒక array ను object లాగా లేదా ఒక object ను array లాగా access చేయడం.
Array item కోసం index ఉపయోగించండి, object property కోసం dot notation ఉపయోగించండి. ఉదాహరణకు cart[0].price.
Interview Questions
Array లో objects ను ఎలా access చేస్తారు?
Answer:
మొదట array index ను, తరువాత object property ను ఉపయోగిస్తారు. ఉదాహరణకు users[0].name మొదటి user యొక్క name ను ఇస్తుంది.
Object లో array ఉంటే దాని value ను ఎలా access చేస్తారు?
Answer:
Object property తరువాత array index ఉపయోగించాలి. ఉదాహరణకు student.marks[1] marks array లోని రెండవ value ను ఇస్తుంది.
Array లోని objects పై calculation చేయడానికి ఏ విధానం ఉపయోగించవచ్చు?
Answer:
ప్రతి object ను ఒక్కొక్కటిగా process చేయడానికి for…of loop ఉపయోగించవచ్చు. Loop లో object properties ను access చేసి calculation చేయవచ్చు.
Exam Questions
కింది data లో మొదటి product యొక్క price ను access చేసే expression ఏది? const products = [{ name: “Phone”, price: 15000 }];
Answer:
సరైన expression products[0].price.
const user = { scores: [70, 80, 90] }; అనే object లో scores array యొక్క length ఎంత?
Answer:
user.scores.length value 3 ను ఇస్తుంది.
ఒక cart item యొక్క total cost కనుగొనడానికి price మరియు quantity properties ఉన్నప్పుడు ఏ expression ఉపయోగించాలి?
Answer:
item.price * item.quantity expression ఉపయోగించాలి.
Quick Revision
-
Array అనేది ordered collection; index zero నుంచి ప్రారంభమవుతుంది.
-
Object data ను key-value pairs రూపంలో store చేస్తుంది.
-
Array లోని object property ను access చేయడానికి array[index].property pattern ఉపయోగించండి.
-
Object లోని array value ను access చేయడానికి object.property[index] pattern ఉపయోగించండి.
-
Nested data పై పని చేసేటప్పుడు data structure ను ముందుగా పరిశీలించండి.
-
Cart total కోసం ప్రతి item యొక్క price ను quantity తో multiply చేసి మొత్తం values ను add చేయండి.
