当前位置:网站首页>对象内多个数组的对应下标相加
对象内多个数组的对应下标相加
2022-07-28 05:19:00 【我想当话家】
对象内多个数组的对应下标相加
问题描述
对象里有n个长度相同的数组(长度为m),将n个数组相同的下标相加,返回数组长度为m
js代码实现
let obj = {
arr1: [1, 2, 3, 4],
arr2: [3, 4, 5, 6],
arr3: [2, 3, 4, 5],
arr4: [1, 1, 1, 1],
};
function getNewArray(obj) {
const arr = Object.values(obj);//将对象里的属性值全部取出放入数组(便于使用数组的api)
let res = [];
for (let i = 0; i < arr[0].length; i++) {
arr.forEach((item) => {
//遍历新得到数组
if (res[i] == undefined) {
res[i] = 0;//当数组某个位置没有值时,将该位置设为0(不然结果就是[NaN,NaN])
}
res[i] += item[i];//每项对应的下标相加
});
}
return res;
}
console.log(getNewArray(obj));
边栏推荐
- How Visio can quickly generate the same pattern and image matrix
- The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem
- Openjudge: stone scissors cloth
- DOM——事件
- c语言:通过一个例子来认识函数栈帧的创建和销毁讲解
- Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later
- Learning of image enhancement evaluation index -- structural similarity SSIM
- openjudge:矩阵乘法
- 对极大似然估计、梯度下降、线性回归、逻辑回归的理解
- media-搭建直播服务器
猜你喜欢

Review of metallurgical physical chemistry ---- gas solid reaction kinetics

Review of metallurgical physical chemistry --- liquid liquid reaction kinetics

冶金物理化学复习 -- 金属电沉积过程中的阴极极化,超电势以及阳极和阳极过程

Arrangement of main drawings of the latest 54 papers of eccv22

How Visio can quickly generate the same pattern and image matrix

Distillation model diagram

NRF51822 回顾总结

Canvas绘图1

顺序表oj题目

Review of metallurgical physical chemistry -- rate equations of complex reactions
随机推荐
Openjudge: judge whether the string is palindrome
蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
openjudge:万年历
ResNet结构对比
Pytorch uses hook to get feature map
C语言回顾(可变参数篇)
pytorch 计算模型的GFlops和total params的方法
The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem
动态卷积的本质
openjudge:找第一个只出现一次的字符
Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade
低照度图像数据集
Delete specific elements in order table OJ
Openjudge: maximum span of string
Oracle create table, delete table, modify table (add field, modify field, delete field) statement summary
C语言回顾(字节对齐篇)
visio如何快速生成相同的图案,生成图像矩阵
openjudge:校园食宿预订系统
对极大似然估计、梯度下降、线性回归、逻辑回归的理解
latex和word之间相互转换