Skip to content

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Notifications You must be signed in to change notification settings

lassiecoder/100daysofjs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 

Repository files navigation

Object manipulation: keys, values, entries

πŸ₯‘ Object.keys, values, entries

πŸ₯‘ Transforming objects


Object.keys, values, entries

Object.keys(), Object.values(), and Object.entries() methods provide ways to work with objects by extracting their keys, values, and key-value pairs, respectively.

Object.keys(): This method returns an array containing the keys of an object.

For example:

const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const keys = Object.keys(person);
console.log(keys); // Output: ['name', 'age', 'city']

Object.values(): This method returns an array containing the values of an object.

For example:

const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const values = Object.values(person);
console.log(values); // Output: ['John', 30, 'New York']

Object.entries(): This method returns an array of arrays, where each inner array contains a key-value pair as [key, value].

For example:

const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const entries = Object.entries(person);
console.log(entries); 
// Output: [['name', 'John'], ['age', 30], ['city', 'New York']]

The above methods are useful for iterating over object properties, performing operations on keys, values, or entries, and manipulating object data in various ways.

Transforming objects

To transform objects using methods like map, filter, and others similar to arrays, we can follow the below steps:

  1. Use Object.entries(obj) to convert the object into an array of key/value pairs.
  2. Apply array methods such as map to transform these key/value pairs.
  3. Finally, use Object.fromEntries(array) to convert the transformed array back into an object.

For example:

let prices = {
  banana: 1,
  orange: 2,
  meat: 4,
};

let doublePrices = Object.fromEntries(
  Object.entries(prices).map(entry => [entry[0], entry[1] * 2])
);

console.log(doublePrices.meat); // Output: 8

About

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published