减少回调函数以从数组中检索属性
Reduce callback function to retrieve properties from an array
给定以下数组,我将编写一个回调函数以减少到 return 一个具有两个键的对象 - 'JavaScript' 和 'python'。密钥应等于使用 'JavaScript' 或 'python' 作为其语言的开发人员的数量。最终控制台日志应为 JavaScript: 4, Python:3.
const developersArray = [
{ name: 'ralph', language: 'javascript' },
{ name: 'gretchen', language: 'javascript' },
{ name: 'alice', language: 'python' },
{ name: 'mohammed', language: 'javascript' },
{ name: 'pat', language: 'python' },
{ name: 'taylor', language: 'python' },
{ name: 'hideo', language: 'javascript' },
];
到目前为止,这就是我想出的。我正在回顾一项旧作业,认为我走在正确的轨道上,但不确定我是否走对了路:
const devLanguageCounts = developersArray.reduce(() => developersArray.language === 'javascript' && 'python' ? ++developersArray : 'javascript', 'python');
非常感谢任何帮助
使用 forEach
的简单方法并检查 hasOwnProperty
语言是否已经存在 res
在其他地方增加它 用 1 初始化它:
const developers = [
{ name: "ralph", language: "javascript" },
{ name: "gretchen", language: "javascript" },
{ name: "alice", language: "python" },
{ name: "mohammed", language: "javascript" },
{ name: "pat", language: "python" },
{ name: "taylor", language: "python" },
{ name: "hideo", language: "javascript" },
];
res = {};
developers.forEach(({ language }) => {
if (res.hasOwnProperty(language)) {
res[language] += 1;
} else {
res[language] = 1;
}
});
console.log(res)
使用 reduce
动态使用 Logical_nullish_assignment 初始 object
如果语言不存在 0 或获取其先前的值,则递增它。
const developers = [
{ name: "ralph", language: "javascript" },
{ name: "gretchen", language: "javascript" },
{ name: "alice", language: "python" },
{ name: "mohammed", language: "javascript" },
{ name: "pat", language: "python" },
{ name: "taylor", language: "python" },
{ name: "hideo", language: "javascript" },
];
const devLanguageCounts = developers.reduce((prev, { language }) => {
prev[language] ??= 0;
prev[language]++;
return prev;
}, {});
console.log(devLanguageCounts);
给定以下数组,我将编写一个回调函数以减少到 return 一个具有两个键的对象 - 'JavaScript' 和 'python'。密钥应等于使用 'JavaScript' 或 'python' 作为其语言的开发人员的数量。最终控制台日志应为 JavaScript: 4, Python:3.
const developersArray = [
{ name: 'ralph', language: 'javascript' },
{ name: 'gretchen', language: 'javascript' },
{ name: 'alice', language: 'python' },
{ name: 'mohammed', language: 'javascript' },
{ name: 'pat', language: 'python' },
{ name: 'taylor', language: 'python' },
{ name: 'hideo', language: 'javascript' },
];
到目前为止,这就是我想出的。我正在回顾一项旧作业,认为我走在正确的轨道上,但不确定我是否走对了路:
const devLanguageCounts = developersArray.reduce(() => developersArray.language === 'javascript' && 'python' ? ++developersArray : 'javascript', 'python');
非常感谢任何帮助
使用 forEach
的简单方法并检查 hasOwnProperty
语言是否已经存在 res
在其他地方增加它 用 1 初始化它:
const developers = [
{ name: "ralph", language: "javascript" },
{ name: "gretchen", language: "javascript" },
{ name: "alice", language: "python" },
{ name: "mohammed", language: "javascript" },
{ name: "pat", language: "python" },
{ name: "taylor", language: "python" },
{ name: "hideo", language: "javascript" },
];
res = {};
developers.forEach(({ language }) => {
if (res.hasOwnProperty(language)) {
res[language] += 1;
} else {
res[language] = 1;
}
});
console.log(res)
使用 reduce
动态使用 Logical_nullish_assignment 初始 object
如果语言不存在 0 或获取其先前的值,则递增它。
const developers = [
{ name: "ralph", language: "javascript" },
{ name: "gretchen", language: "javascript" },
{ name: "alice", language: "python" },
{ name: "mohammed", language: "javascript" },
{ name: "pat", language: "python" },
{ name: "taylor", language: "python" },
{ name: "hideo", language: "javascript" },
];
const devLanguageCounts = developers.reduce((prev, { language }) => {
prev[language] ??= 0;
prev[language]++;
return prev;
}, {});
console.log(devLanguageCounts);