The _.mapObject() function is similar to the map but used for the object. This function transforms each value of the object based on the given function/operation.
Syntax:
_.mapObject(object, iteratee, [context])
Parameters: This function accepts three parameters as mentioned above and described below:
- object: It contains the object element that holds the elements of key and value pair.
- iteratee: It is the function which is used to take all the elements of the list and it also remember all the returned value.
- context: This parameter is used to display the content.
Return Value: It returns the transform value of each object element.
Example 1:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" Â Â Â Â Â Â Â Â Â Â Â Â Â src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â var res = _.mapObject({ num1: 10, num2: 15 }, Â Â Â Â Â Â Â Â Â Â Â Â function (value, key) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return value * 10; Â Â Â Â Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Â Â console.log(res); Â Â Â Â </ script > </ body > Â Â </ html > |
Output:
Example 2:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" Â Â Â Â Â Â Â Â Â Â Â Â Â src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â _.mapObject({ num1: 10, num2: 15, num3: 20, num4: 25, num5: 30 }, Â Â Â Â Â Â Â Â Â Â Â Â Â Â function (value, key) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â console.log(key + ': ' + (value + 10)); Â Â Â Â Â Â Â Â Â Â Â Â }); Â Â Â Â </ script > </ body > Â Â </ html > |
Output: