尝试 return 基于条件的对象数组中的 filter() 值
Trying to return a filter() value from an array of objects based on conditionals
我正在尝试 return 仅将我的对象的一部分作为基于条件的数组,但我有点难过。
我有一个对象数组,我想 return 每个键的名称数组:它们适合的值。
我确实只得到了独特的配对,但我也在 return 吃食物 key:value 并且所有食物仍然在一个对象而不是新数组中。一些见解将不胜感激。编码较新。
const organizeNames = function (foods) {
let foodNames = foods.filter((names) => {
if (names.food === 'oranges') {
return names.name;
}
});
console.log(foodNames);
};
console.log(
organizeNames([
{ name: 'Samuel', food: 'oranges' },
{ name: 'Victoria', food: 'pizza' },
{ name: 'Karim', food: 'pizza' },
{ name: 'Donald', food: 'pizza' },
])
);
你真的很接近这里。您需要合并的是 .map()
以将您的对象列表映射到名称列表。您的过滤器部分起作用,部分是偶然的,所以我已将其修复为更正确(return names.food === 'oranges'
),然后一旦您拥有与食物匹配 'oranges' 的对象列表,您就可以映射它通过 .map(names => names.name)
将列表过滤为名称列表
const organizeNames = function (foods) {
let foodNames = foods.filter((names) => {
// only keep items whose .food property === 'oranges'
return names.food === 'oranges'; // returns true or false
}).map(names => {
// map the filtered list of objects into a list of names by
// returning just the object's .name property
return names.name;
});
return foodNames;
};
console.log(
organizeNames([
{ name: 'Samuel', food: 'oranges' },
{ name: 'Victoria', food: 'pizza' },
{ name: 'Karim', food: 'pizza' },
{ name: 'Donald', food: 'pizza' },
])
);
filter()
回调函数应该只是returntrue
或false
。然后您可以使用 map()
从每个筛选的项目中获取特定的 属性。
const organizeNames = function(foods) {
let foodNames = foods.filter((names) => names.food == 'oranges').map(names => names.name);
return foodNames;
}
console.log(
organizeNames([{
name: 'Samuel',
food: 'oranges'
},
{
name: 'Victoria',
food: 'pizza'
},
{
name: 'Karim',
food: 'pizza'
},
{
name: 'Donald',
food: 'pizza'
},
])
);
我正在尝试 return 仅将我的对象的一部分作为基于条件的数组,但我有点难过。
我有一个对象数组,我想 return 每个键的名称数组:它们适合的值。
我确实只得到了独特的配对,但我也在 return 吃食物 key:value 并且所有食物仍然在一个对象而不是新数组中。一些见解将不胜感激。编码较新。
const organizeNames = function (foods) {
let foodNames = foods.filter((names) => {
if (names.food === 'oranges') {
return names.name;
}
});
console.log(foodNames);
};
console.log(
organizeNames([
{ name: 'Samuel', food: 'oranges' },
{ name: 'Victoria', food: 'pizza' },
{ name: 'Karim', food: 'pizza' },
{ name: 'Donald', food: 'pizza' },
])
);
你真的很接近这里。您需要合并的是 .map()
以将您的对象列表映射到名称列表。您的过滤器部分起作用,部分是偶然的,所以我已将其修复为更正确(return names.food === 'oranges'
),然后一旦您拥有与食物匹配 'oranges' 的对象列表,您就可以映射它通过 .map(names => names.name)
const organizeNames = function (foods) {
let foodNames = foods.filter((names) => {
// only keep items whose .food property === 'oranges'
return names.food === 'oranges'; // returns true or false
}).map(names => {
// map the filtered list of objects into a list of names by
// returning just the object's .name property
return names.name;
});
return foodNames;
};
console.log(
organizeNames([
{ name: 'Samuel', food: 'oranges' },
{ name: 'Victoria', food: 'pizza' },
{ name: 'Karim', food: 'pizza' },
{ name: 'Donald', food: 'pizza' },
])
);
filter()
回调函数应该只是returntrue
或false
。然后您可以使用 map()
从每个筛选的项目中获取特定的 属性。
const organizeNames = function(foods) {
let foodNames = foods.filter((names) => names.food == 'oranges').map(names => names.name);
return foodNames;
}
console.log(
organizeNames([{
name: 'Samuel',
food: 'oranges'
},
{
name: 'Victoria',
food: 'pizza'
},
{
name: 'Karim',
food: 'pizza'
},
{
name: 'Donald',
food: 'pizza'
},
])
);