Skip to content

Lesson 2: map #61

@seemcat

Description

@seemcat

Takes in an object and a function and calls the function as many times as there are key/value pairs with each key, value. It's like you're creating a forEach function for objects. For each key/value pair in the object, you'll be doing calling the function to each of those key/value pairs. Also, map is a native method in Javascript which means doing the same thing to every element in the array. So you're essentially writing a map method for objects!

Example 1:

const obj1 = {
     name: 'Maricris',
     age: 24,
};
const func1 = (key, value) => {
     console.log(`Key/Value Pair = ${key}: ${value}`);
};
Input: (obj1, func1)
Output: 
Key/Value Pair = Name: Maricris
Key/Value Pair = Age: 24

Example 2:

const obj2 = {
     curriculum: 'HTML',
     challenges: 3,
     students: 'Kinue + Saumya +Amey',
     level: 5,
};
const func2 = (key, value) => {
     console.log(`Key/Value Pair = ${key}: ${value}`);
};
Input: (obj2, func2)
Output: 
Key/Value Pair = curriculum: HTML
Key/Value Pair = challenges: 3
Key/Value Pair = students: Kinue + Saumya + Amey
Key/Value Pair = level: 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions