[go: up one dir, main page]

0% found this document useful (0 votes)
3 views3 pages

JavaScript Object Array CheatSheet - Google Docs

This document is a cheat sheet for JavaScript objects and arrays, detailing various methods available for each. It includes examples for object methods like Object.keys, Object.values, and Object.entries, as well as array methods such as push, pop, and map. Additionally, it provides tips for checking if an object has a key and looping through objects and arrays.

Uploaded by

arjun.ks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

JavaScript Object Array CheatSheet - Google Docs

This document is a cheat sheet for JavaScript objects and arrays, detailing various methods available for each. It includes examples for object methods like Object.keys, Object.values, and Object.entries, as well as array methods such as push, pop, and map. Additionally, it provides tips for checking if an object has a key and looping through objects and arrays.

Uploaded by

arjun.ks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

J‭ avaScript Cheat Sheet – Objects &‬

‭Arrays‬
‭🔶 JavaScript Object Methods‬
‭ bject.keys(obj): Returns an array of object’s keys‬
O
‭Example: Object.keys({a:1, b:2}) // ['a', 'b']‬

‭ bject.values(obj): Returns an array of object’s values‬


O
‭Example: Object.values({a:1, b:2}) // [1, 2]‬

‭ bject.entries(obj): Returns an array of key-value pairs‬


O
‭Example: Object.entries({a:1}) // [['a', 1]]‬

‭ bject.assign(target, source): Copies properties from source to target‬


O
‭Example: Object.assign({}, {a:1}) // {a:1}‬

‭ bject.freeze(obj): Makes object immutable‬


O
‭Example: Object.freeze(obj)‬

‭ bject.seal(obj): Prevents adding/removing keys but can update values‬


O
‭Example: Object.seal(obj)‬

E‭ xample:‬
‭const user = { name: 'Arjun', age: 25 };‬
‭console.log(Object.keys(user)); // ['name', 'age']‬
‭console.log(Object.values(user)); // ['Arjun', 25]‬
‭console.log(Object.entries(user)); // [['name', 'Arjun'], ['age', 25]]‬

‭🔷 JavaScript Array Methods‬


‭ ush(): Adds to end of array‬
p
‭Example: arr.push(4)‬

‭ op(): Removes last element‬


p
‭Example: arr.pop()‬

s‭ hift(): Removes first element‬


‭Example: arr.shift()‬

‭ nshift(): Adds to beginning of array‬


u
‭Example: arr.unshift(0)‬
s‭ plice(start, delete, add): Removes/adds elements‬
‭Example: arr.splice(1, 1, 100)‬

s‭ lice(start, end): Returns a new array from range‬


‭Example: arr.slice(1, 3)‬

c‭ oncat(): Merges arrays‬


‭Example: [1, 2].concat([3, 4])‬

j‭oin(): Joins array into string‬


‭Example: arr.join('-')‬

i‭ndexOf(): Finds index of element‬


‭Example: arr.indexOf(3)‬

i‭ncludes(): Checks if array contains value‬


‭Example: arr.includes(2)‬

f‭ orEach(): Iterates over array‬


‭Example: arr.forEach(x => console.log(x))‬

‭ ap(): Transforms array, returns new array‬


m
‭Example: arr.map(x => x * 2)‬

‭ lter(): Returns elements that match condition‬


fi
‭Example: arr.filter(x => x > 2)‬

r‭ educe(): Reduces array to single value‬


‭Example: arr.reduce((a, b) => a + b)‬

‭ nd(): Finds first matching value‬


fi
‭Example: arr.find(x => x > 3)‬

‭ ndIndex(): Index of first matching element‬


fi
‭Example: arr.findIndex(x => x === 5)‬

s‭ ort(): Sorts array‬


‭Example: arr.sort()‬

r‭ everse(): Reverses the array‬


‭Example: arr.reverse()‬

E‭ xample:‬
‭const nums = [1, 2, 3, 4];‬
‭const doubled = nums.map(x => x * 2); // [2, 4, 6, 8]‬
‭const evens = nums.filter(x => x % 2 === 0); // [2, 4]‬
c‭ onst total = nums.reduce((sum, val) => sum + val, 0); // 10‬
‭const firstGreaterThan2 = nums.find(x => x > 2); // 3‬

‭🧠 Bonus – Some Useful Tips‬


‭ heck if object has key:‬
C
‭const obj = { name: "Arjun" };‬
‭console.log("name" in obj); // true‬

L‭ oop through object:‬


‭for (let key in obj) {‬
‭console.log(key, obj[key]);‬
‭}‬

L‭ oop through array:‬


‭arr.forEach((val, idx) => {‬
‭console.log(idx, val);‬
‭});‬

You might also like