只是想知道是否有人可以向我解释这一点:我已经设法通过在地图中key
手动声明“function
”来实现我之后的结果。
我很难理解如何在地图函数中传递密钥,因此我可以轻松调用valueMapper('key')
以获得相同的结果(下面的示例)。
我发现了一些解决方案,但它们似乎都没有意义。请为方法添加一些背景知识,我对该过程比解决方案更感兴趣。
感谢您的帮助!
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
]
// write a function call valueMapper
// valueMapper takes one argument which is the name of a key
// it returns an array of all the values in the users array that correspond to that key
// examples:
// valueMapper('favoriteFood')
// -> ['Pizza', 'Curry', 'Fish', 'Steak', 'Pizza']
// valueMapper('firstName')
// -> ['Pete', 'Lisa', 'Bob', 'Claire', 'Adam']
/* #1st solution, I managed to get the answers I'm looking for, but I haven't defined
the valueMapper function and I haven't passed it a key yet */
var lastName = users.map(function(users) {
return users['lastName'];
})
console.log(lastName);
/* #2nd solution is an attempt to create an anonymous function so that
I can pass through a key to the function in order to read the same results*/
function valueMapper() {Object.keys(users).map(function(key) {
return users[key];
});
}
console.log(valueMapper('firstName'));
答案 0 :(得分:1)
当你map
一个带有函数fn
的数组时,你正在创建一个新数组,其中包含对该数组中每个项目调用fn
的结果。
[a, b, c].map(fn) // => [fn(a), fn(b), fn(c)]
// e.g.
[1.5, 2.2, 0.3].map(Math.floor) // => [1, 2, 0]
您正在寻找的功能存在于流行的JavaScript库中,有时也称为pluck
。它使用映射到" pluck"集合中每个对象的字段:
var pluck = function (collection, field) {
return collection.map(function (item) {
return item[field];
});
};
// e.g.
var albums = [
{ title: 'Colony', release: '1999' },
{ title: 'Clayman', release: '2000' },
{ title: 'Reroute to Remain', release: '2002' }
];
pluck(albums, 'title'); // => ['Colony', 'Clayman', 'Reroute to Remain']
在您的具体情况下,它看起来像这样:
var valueMapper = function (key) {
return users.map(function (user) {
return user[key];
});
};
答案 1 :(得分:0)
valueMapper
),那么它不是一个匿名函数。当您将属性值作为参数传递给函数时,不需要使用Object.Keys
迭代属性。但是,作为gaurd,您可以检查传入的属性名称是否与对象上的现有属性匹配。
您可能正在寻找如下所示的解决方案。
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
]
// write a function call valueMapper
// valueMapper takes one argument which is the name of a key
// it returns an array of all the values in the users array that correspond to that key
// examples:
// valueMapper('favoriteFood')
// -> ['Pizza', 'Curry', 'Fish', 'Steak', 'Pizza']
// valueMapper('firstName')
// -> ['Pete', 'Lisa', 'Bob', 'Claire', 'Adam']
/* #1st solution, I managed to get the answers I'm looking for, but I haven't defined
the valueMapper function and I haven't passed it a key yet */
var lastName = users.map(function(users) {
return users['lastName'];
})
console.log(lastName);
/* #2nd solution is an attempt to create an anonymous function so that
I can pass through a key to the function in order to read the same results*/
function valueMapper(passedInKey) {
return users.map(function(user) {
if(user.hasOwnProperty(passedInKey)){
return user[passedInKey];
}
});
}
console.log(valueMapper('firstName'));
答案 2 :(得分:0)
一种解决方案是构建一个看似如下的hash
表:
{
key1: Array('all values of key1'),
key2: Array('all values of key2'),
...}
然后使用此哈希表快速获得结果。这将是一个很好的解决方案,因为您需要构建一次哈希,然后可以使用O(1)
的复杂度检索结果。
见下面的例子:
var users = [
{firstName: 'Pete', lastName: 'Barrat', favoriteFood: 'Pizza', age: 30},
{firstName: 'Lisa', lastName: 'Jenkins', favoriteFood: 'Curry', age: 34},
{firstName: 'Bob', lastName: 'Yates', favoriteFood: 'Fish', age: 54},
{firstName: 'Claire', lastName: 'Smith', favoriteFood: 'Steak', age: 21},
{firstName: 'Adam', lastName: 'Johnson', favoriteFood: 'Pasta', age: 27}
]
var hash = {};
users.forEach((user) => {
Object.keys(user).forEach((key) => {
hash[key] = hash[key] || new Array();
hash[key].push(user[key]);
})
});
function getKey(key) {
return hash[key] || [];
}
console.log(getKey('age'));
console.log(getKey('favoriteFood'));
console.log(getKey('hey'));