javascript - Using .map method to update the object that I'm evaluating
问题描述
I am executing the map method on an array selectedItem
where selectedItem
has a string property nodeType
and an object property items
:
items = { computers: 0, drives: 0, files: 0, folders: 0 }
selectedItem?.map(item => {
switch (item.nodeType) {
case 'FileType':
return (items.files += 1);
case 'FolderType':
return (items.folders += 1);
case 'DriveType':
return (items.drives += 1);
case 'ComputerType':
return (items.computers += 1);
default:
return;
}
I know I should be able to replace the switch statement by using the built-in functionality of the map method (hasOwnProperty
), but I've only seen examples where Object literals are used to match on in order to return the string, like this:
const itemTypes = {
file: 'file',
folder: 'folder',
drive: 'drive',
computer: 'computer'
}
However, upon a .map(expression)
match I want to execute a simple incrementing logic to +=1
to a property within the object I'm evaluating.
I've looked at .filter()
and .reduce()
but of course don't apply since they return a different array. I'm sure it may have to deal with writing the correct function within the .map(expression)
but not sure how to do that.
I've looked at these SO posts, but they either don't apply or don't get me all the way there. The second link is very close to what I'm looking for but not sure how to apply it to my code:
How would I do that using only map and not nesting a switch within the map function?
解决方案
map() is used to create a new array. It is not useful when you want to update an object.
You should be using reduce to count up all the types
const selectedItem = [
{ nodeType: 'FolderType' },
{ nodeType: 'DriveType' },
{ nodeType: 'DriveType' },
{ nodeType: 'FolderType' },
{ nodeType: 'ComputerType' },
];
const types = {
FileType: 'files',
FolderType: 'folders',
DriveType: 'drives',
ComputerType: 'computers'
};
const items = {
computers: 0,
drives: 0,
files: 0,
folders: 0
};
selectedItem?.reduce((obj, item) => {
const type = types[item.nodeType];
if (type) obj[type]++;
return obj;
}, items);
console.log(items);
or forEach
const selectedItem = [
{ nodeType: 'FolderType' },
{ nodeType: 'DriveType' },
{ nodeType: 'DriveType' },
{ nodeType: 'FolderType' },
{ nodeType: 'ComputerType' },
];
const types = {
FileType: 'files',
FolderType: 'folders',
DriveType: 'drives',
ComputerType: 'computers'
};
const items = {
computers: 0,
drives: 0,
files: 0,
folders: 0
};
selectedItem?.forEach((item) => {
const type = types[item.nodeType];
if (type) items[type]++;
});
console.log(items);
推荐阅读
- python - 气流将长时间运行的任务标记为失败
- node.js - 如何从firebase云功能向我的应用引擎django应用程序发出请求承诺请求
- c++ - SChannel 中的密码套件选择
- java - 对数据结构中的特定元素进行排名 - 是一种更有效的方法吗?
- javascript - 如何在heroku上全局安装npm包?
- c# - 需要帮助使用 C# 从 CSV 文件中删除特定的命名列
- flutter - Flutter:限制 webview_flutter 长按手势或打开链接预览
- python - 如何将 sns.facetgrid 保存为 pdf
- javascript - 如何通过单击另一个字段来获取 id 的输入
- google-cloud-platform - Google Cloud Platform 项目配额 - 按组织或组织中的用户或两者