JavaScript માં Objects
JavaScript objects સંબંધિત ડેટાને key-value જોડી તરીકે સંગ્રહિત કરે છે. દરેક key તેની value માટે એક અનન્ય (unique) ઓળખકર્તા તરીકે કાર્ય કરે છે.
ઓબ્જેક્ટમાં values માં સમાવેશ થાય છે:
- Primitives
- અન્ય objects
- Functions (જેમને methods કહેવામાં આવે છે)
Objects એ mutable છે. તમે કોઈપણ સમયે properties ઉમેરી, બદલી અથવા દૂર કરી શકો છો. આ તમને ડેટા અને વર્તણૂક (behavior) ને એકસાથે જૂથબદ્ધ કરવામાં મદદ કરે છે.
Objects બનાવવા માટેના બે રસ્તાઓ:
Object Literals properties ને સીધી રીતે વ્યાખ્યાયિત કરવા માટે curly braces નો ઉપયોગ કરો. Example: const user = { name: "Vidhya", age: 23, job: "Developer" };
New Object Constructor Object() constructor નો ઉપયોગ કરો. Example: const user = new Object(); user.name = "Alex"; user.age = 4;
Objects સાથે કેવી રીતે કામ કરવું:
- ડેટા એક્સેસ કરવો: dot notation (user.name) અથવા bracket notation (user["name"]) નો ઉપયોગ કરો.
- ડેટામાં ફેરફાર કરવો: property value ને ફરીથી અસાઇન કરો (user.age = 25).
- ડેટા ઉમેરવો: નવી key સેટ કરવા માટે dot અથવા bracket notation નો ઉપયોગ કરો.
- ડેટા દૂર કરવો: delete operator નો ઉપયોગ કરો.
- Properties તપાસવી: "in" operator અથવા hasOwnProperty() method નો ઉપયોગ કરો.
- Looping: બધી properties માંથી પસાર થવા માટે for...in loop નો ઉપયોગ કરો.
- Merging: Object.assign() અથવા spread syntax (...) નો ઉપયોગ કરો.
- Properties ગણવા માટે: Object.keys() નો ઉપયોગ કરો.
તમારા references ને સ્થિર રાખવા માટે હંમેશા તમારા objects ને const સાથે declare કરો.
Source: https://dev.to/vidhya_murali_5aabe7784bd/objects-in-javascript-5600