Home » Tech Tutorials » Web Development » JavaScript » Accessing and Updating JavaScript Arrays

Accessing and Updating JavaScript Arrays

JavaScript Arrays లోని Values ను Access చేయడం

Array అనేది ఒకే variable లో అనేక values ను క్రమంగా నిల్వ చేయడానికి ఉపయోగించే data structure. Array లోని ప్రతి value కు ఒక index ఉంటుంది. మొదటి value index 0, రెండవ value index 1 గా ఉంటుంది.

ఒక array value ను access చేయడానికి array పేరు తరువాత square brackets లో index ను రాయాలి. ఉదాహరణకు, fruits[0] మొదటి value ను ఇస్తుంది.

Array Value ను Access చేయడం

ఒక array లోని value ను చదవడానికి ఈ syntax ఉపయోగించవచ్చు: arrayName[index].

Index array length కంటే ఎక్కువగా ఉంటే JavaScript సాధారణంగా undefined ను return చేస్తుంది.

Array Value ను Update చేయడం

Array లో ఇప్పటికే ఉన్న value ను మార్చడానికి, కావాల్సిన index కు కొత్త value ను assignment operator = తో assign చేయాలి. ఉదాహరణకు, fruits[1] = “Orange” అని రాస్తే index 1 వద్ద ఉన్న value మారుతుంది.

ఇలా update చేసినప్పుడు కొత్త array సృష్టించబడదు. అదే array లోని ఆ స్థానంలో ఉన్న value replace అవుతుంది.

Array Length

length property ద్వారా array లో ఎన్ని values ఉన్నాయో తెలుసుకోవచ్చు. ఉదాహరణకు, fruits.length array లోని మొత్తం elements సంఖ్యను ఇస్తుంది.

గుర్తుంచుకోవాల్సిన విషయాలు

  • Array index ఎప్పుడూ 0 నుండి ప్రారంభమవుతుంది.
  • Value access చేయడానికి arrayName[index] ఉపయోగించాలి.
  • Value update చేయడానికి arrayName[index] = newValue ఉపయోగించాలి.
  • చివరి index ఎప్పుడూ length – 1 అవుతుంది.

Simple Explanation

Array ను ఒక వరుస lockers లాగా ఊహించుకోండి. ప్రతి locker కు ఒక number ఉంటుంది. మొదటి locker number 0. ఒక locker లో ఉన్న వస్తువును చూడాలంటే దాని number ఉపయోగిస్తాం. వస్తువును మార్చాలంటే అదే number ఉన్న locker లో కొత్త వస్తువును ఉంచుతాం.

అదేవిధంగా, JavaScript లో colors[0] మొదటి value ను access చేస్తుంది. colors[0] = “Green” అని రాస్తే మొదటి value update అవుతుంది.

Real-Life Example

ఒక shopping cart లో products array ఉందని అనుకుందాం: [“Book”, “Pen”, “Bag”]. Customer మొదటి product పేరు చూడాలంటే index 0 ను access చేయాలి. Customer product మార్చితే, ఆ product ఉన్న index కు కొత్త product పేరు assign చేయవచ్చు.

ఇది web application లో user profile information, product lists, scores లేదా menu items update చేయడానికి ఉపయోగపడుతుంది.

Code Examples

Array Value ను Access చేసి Update చేయడం

const fruits = ["Apple", "Banana", "Mango"];

console.log(fruits[1]);

fruits[1] = "Orange";

console.log(fruits.join(", "));

మొదట fruits[1] ద్వారా రెండవ value అయిన Banana ను access చేస్తున్నాం. తరువాత index 1 వద్ద ఉన్న value ను Orange తో update చేస్తున్నాం. join() ను values ను readable text గా display చేయడానికి ఉపయోగిస్తున్నాం.

Banana
Apple, Orange, Mango

మొదటి మరియు చివరి Array Values ను Access చేయడం

const scores = [75, 88, 92, 64];

console.log(scores[0]);
console.log(scores[scores.length - 1]);

scores[3] = 70;
console.log(scores.join(" | "));

scores[0] మొదటి score ను ఇస్తుంది. చివరి value ను access చేయడానికి scores.length – 1 ఉపయోగించాం. తరువాత index 3 వద్ద ఉన్న score ను 64 నుండి 70 గా update చేశాం.

75
64
75 | 88 | 92 | 70

Array లోని అనేక Values ను Update చేయడం

const tasks = ["Study", "Exercise", "Shopping"];

tasks[0] = "Read JavaScript";
tasks[2] = "Buy groceries";

console.log(tasks.join(", "));

ఇక్కడ index 0 మరియు index 2 వద్ద ఉన్న values ను విడివిడిగా update చేశాం. Index ఆధారంగా array లోని ఏ స్థానంలోని value అయినా మార్చవచ్చు.

Read JavaScript, Exercise, Buy groceries

Line-by-Line Explanation

const fruits = [“Apple”, “Banana”, “Mango”];

ఈ line లో మూడు string values తో fruits అనే array సృష్టించాం. Apple index 0, Banana index 1, Mango index 2 వద్ద ఉన్నాయి.

ఈ ఖాళీ line code ను చదవడానికి సులభంగా చేయడానికి ఉపయోగించిన spacing మాత్రమే.

console.log(fruits[1]);

fruits[1] ద్వారా array లోని index 1 value ను access చేస్తున్నాం. అందువల్ల console లో Banana కనిపిస్తుంది.

ఈ ఖాళీ line statements మధ్య readability కోసం ఉంది.

fruits[1] = “Orange”;

ఈ line index 1 వద్ద ఉన్న Banana value ను Orange తో replace చేస్తుంది. ఇది existing array ను update చేస్తుంది.

ఈ ఖాళీ line code sections మధ్య spacing కోసం ఉంది.

console.log(fruits.join(“, “));

join(“, “) array values ను comma మరియు space తో కలిపి ఒక text గా తయారు చేస్తుంది. Update చేసిన తరువాత array values console లో కనిపిస్తాయి.

Try It

Try this JavaScript code:

Example

const studentNames = ["Ravi", "Sita", "Arjun"];

console.log(studentNames[0]);

studentNames[1] = "Meena";

console.log(studentNames.join(", "));

Try It Yourself »

Practice

Practice:

products array లోని index 2 value ను console లో చూపించండి. తరువాత index 0 value ను “Drawing book” గా update చేసి, మొత్తం array ను comma తో display చేయండి.

Hint:

Value access చేయడానికి products[2] ఉపయోగించండి. Update చేయడానికి products[0] = “Drawing book” రాయండి.

Common Mistakes

మొదటి array value కోసం array[1] ఉపయోగించడం.

JavaScript arrays 0-based indexing ఉపయోగిస్తాయి. కాబట్టి మొదటి value కోసం array[0] ఉపయోగించాలి.

Value update చేసేటప్పుడు index లేకుండా మొత్తం array name కు కొత్త value assign చేయడం.

ఒక నిర్దిష్ట value మార్చడానికి దాని index తప్పనిసరిగా ఇవ్వాలి. ఉదాహరణకు items[1] = “New item”.

చివరి index ను array.length అని అనుకోవడం.

చివరి index ఎప్పుడూ array.length – 1. ఎందుకంటే indexing 0 నుండి ప్రారంభమవుతుంది.

లేని index ను access చేస్తే తప్పనిసరిగా error వస్తుందని అనుకోవడం.

సాధారణ array access లో లేని index కోసం JavaScript సాధారణంగా undefined ను return చేస్తుంది.

Interview Questions

JavaScript array లో మొదటి element ను ఎలా access చేస్తారు?

Answer:

మొదటి element ను arrayName[0] ద్వారా access చేస్తారు. JavaScript array indexing 0 నుండి ప్రారంభమవుతుంది.

Array లోని ఒక value ను ఎలా update చేస్తారు?

Answer:

కావాల్సిన index కు assignment చేయాలి. ఉదాహరణకు items[1] = “Updated value”.

Array లోని చివరి element ను ఎలా access చేయవచ్చు?

Answer:

arrayName[arrayName.length – 1] expression ఉపయోగించి చివరి element ను access చేయవచ్చు.

లేని array index ను access చేస్తే ఏమి వస్తుంది?

Answer:

సాధారణంగా undefined value వస్తుంది.

Exam Questions

ఈ code output ఏమిటి? const numbers = [10, 20, 30]; numbers[1] = 25; console.log(numbers.join(“-“));

Answer:

Output: 10-25-30. Index 1 వద్ద ఉన్న 20 value, 25 గా update అవుతుంది.

const colors = [“Red”, “Blue”, “Green”]; లో చివరి value ను access చేయడానికి ఏ expression ఉపయోగించాలి?

Answer:

colors[colors.length – 1] expression ఉపయోగించాలి.

Array indexing 0 నుండి ప్రారంభమవుతుందంటే ఏమిటి?

Answer:

మొదటి element index 0, రెండవ element index 1, మూడవ element index 2 గా ఉంటాయి అని అర్థం.

Quick Revision

  • Array అనేది అనేక values ను క్రమంగా నిల్వ చేసే data structure.
  • JavaScript arrays లో index 0 నుండి ప్రారంభమవుతుంది.
  • Value access syntax: arrayName[index].
  • Value update syntax: arrayName[index] = newValue.
  • మొదటి element index 0 వద్ద ఉంటుంది.
  • చివరి element ను arrayName[arrayName.length – 1] తో access చేయవచ్చు.
  • లేని index ను access చేస్తే సాధారణంగా undefined వస్తుంది.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top