javascript - 请告诉我,如何创建一个将对象的所有数字属性乘以 2 的函数
问题描述
创建一个函数 multiplyNumeric(objectExample) 将 objectExample 的所有数字属性乘以 2。我的错误在哪里?
let objectExample = {
width: 200,
height: 300,
title: 'example'
}
let multiplyNumeric = (key, object) => {
for (let key in object) {
if (typeof object.key === 'number') {
object.key *= 2;
}
}
}
multiplyNumeric(objectExample);
console.log(objectExample);
非常感谢您。
解决方案
let objectExample = {
width: 200,
height: 300,
title: 'example'
}
let multiplyNumeric = obj => {
for (let [key, value] of Object.entries(obj)) {
if (typeof value === 'number') {
obj[key] = value * 2;
}
}
}
multiplyNumeric(objectExample);
console.log(objectExample);
推荐阅读
- flutter - 自动对焦重置到文本字段的开头
- amazon-s3 - 为什么使用 pyspark 从 s3 读取时数据文件头作为数据行读取
- npm - 如何解决 npm ERR!安装 webpack 时的代码 1
- vim - 如何在 Vim 中折叠 C 风格的多行注释 /*...*/?
- cordova - Ionic 5 Stripe 如何使用success_url将用户重定向回应用程序?
- c++ - C++:这个浮点错误问题的解决方案?
- python-3.x - Python pyppeteer,如何使用 socks 代理
- database - 如何将金融交易映射到类别
- c - 随机正态分布发生器比较
- sql - 在 Django 中将 format() 函数与 pyodbc 一起使用时出现 SQL 错误