Google News
logo
Underscore.js - Interview Questions
What is the Mapping Objects in Underscore.js?
keys _.keys(object)
Retrieve all the names of the object's own enumerable properties.
_.keys({one: 1, two: 2, three: 3});
=> ["one", "two", "three"]

allKeys _.allKeys(object)
Retrieve all the names of object's own and inherited properties.
function Stooge(name) {
  this.name = name;
}
Stooge.prototype.silly = true;
_.allKeys(new Stooge("Moe"));
=> ["name", "silly"]

values _.values(object)
Return all of the values of the object's own properties.
_.values({one: 1, two: 2, three: 3});
=> [1, 2, 3]

mapObject _.mapObject(object, iteratee, [context])
Like map, but for objects. Transform the value of each property in turn.
_.mapObject({start: 5, end: 12}, function(val, key) {
  return val + 5;
});
=> {start: 10, end: 17}

pairs _.pairs(object)
Convert an object into a list of [key, value] pairs. The opposite of object.
_.pairs({one: 1, two: 2, three: 3});
=> [["one", 1], ["two", 2], ["three", 3]]

invert _.invert(object)
Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object's values should be unique and string serializable.
_.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"});
=> {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};

create _.create(prototype, props)
Creates a new object with the given prototype, optionally attaching props as own properties. Basically, Object.create, but without all of the property descriptor jazz.
var moe = _.create(Stooge.prototype, {name: "Moe"});

functions _.functions(object) Alias :
methods
Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object.
_.functions(_);
=> ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...

findKey _.findKey(object, predicate, [context])
Similar to _.findIndex but for keys in objects. Returns the key where the predicate truth test passes or undefined. predicate is transformed through iteratee to facilitate shorthand syntaxes.
Advertisement