JavaScript convert An Object into An Array ( #shorts )
Hi Friends,
JavaScript convert An Object into An Array
In JavaScript, you can convert an object into an array using several methods. Here are the examples:
Using Object.entries()
The Object.entries() method returns an array of a given object's own list string-keyed property [key, value] pairs. You can then map over the array and extract just the values or keys as needed.
Example:
const obj = {a: 1, b: 2, c: 3, d: 4};
const arr = Object.entries(obj).map(([key, value]) => ({ key, value }));
console.log(arr); // [{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 }, { key: 'd', value: 4 }]
Using Object.keys()
The Object.keys() method returns an array of a given object's own enumerable property names.
Example:
const obj = {a: 1, b: 2, c: 3, d: 4};
const arr = Object.keys(obj).map(key => ({ key, value: obj[key] }));
console.log(arr); // [{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 }, { key: 'd', value: 4 }]
Using Object.values()
The Object.values() method returns an array of a given object's own enumerable property values.
Example:
const obj = {a: 1, b: 2, c: 3, d: 4};
const arr = Object.values(obj);
console.log(arr); // [1, 2, 3, 4]
#javascript #js #objective #object #array #popular #trending #viral #shorts #short #trend #trendingnow #trendingshort #trendingshorts #popular_status #popularvideo #shortstrending #shortsvideo #javascriptlearning #learning #learn #learnjavascript #success #javascriptarrays #javascriptobject #jsarray #jsobject #trendjs #trendingjs #trendingjavascript #princecheema_official @princecheema_official
-
2:12:05
Tactical Advisor
9 hours agoGun Giveaway Info & New Build Spotlight - Vault Room Live Stream - Vault Room Live Stream
59.7K21 -
1:15:27
JoBlo Originals
1 day agoFriday Night Flicks!
66.3K11 -
1:00:41
Trumpet Daily
1 day ago $6.79 earnedJoe Biden’s Demeanor Is ‘Not of This Earth’ - Trumpet Daily | July 5, 2024
85.5K101 -
12:05
Space Ice
1 day agoStar Wars The Acolyte Is So Good The Fans Failed The Critics - Best Movie Ever
78.4K102 -
30:57
Degenerate Plays
1 day agoDid Gaming Peak Back Then? - Stellar Blade : Part 6
80.9K1 -
8:03
Kicking Mustang
1 day ago $1.68 earnedUsing my Ghillie Suit to Sneak Attack Clueless Enemies..
70.9K17 -
2:47
Maicy Renee
1 day agoSounds Like a Terrible Night!
62.8K30 -
18:23
Crypto Crew University
1 day ago $1.08 earnedWARNING: BITCOIN ABOUT TO DO SOMETHING THAT CAUSED MEGA CRASH LAST TIME – CAN IT BE AVOIDED
61.4K27 -
2:52
Hack
19 hours agoAn unusual idea with a bottle and a can of spray paint
49.6K11 -
25:18
Brewzle
1 day agoI Went Bourbon Hunting in a Tourist Trap | Hilton Head, SC
39.4K