当前位置:网站首页>Add the corresponding subscripts of multiple arrays in the object
Add the corresponding subscripts of multiple arrays in the object
2022-07-28 05:48:00 【I want to be a talker】
Add the corresponding subscripts of multiple arrays in the object
Problem description
There are n An array of the same length ( The length is m), take n Add the same subscripts of arrays , The length of the returned array is m
js Code implementation
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);// Take out all the attribute values in the object and put them into the array ( Easy to use array api)
let res = [];
for (let i = 0; i < arr[0].length; i++) {
arr.forEach((item) => {
// Traverse the newly obtained array
if (res[i] == undefined) {
res[i] = 0;// When there is no value at a certain position in the array , Set this position to 0( Otherwise, the result is [NaN,NaN])
}
res[i] += item[i];// Add the subscripts corresponding to each item
});
}
return res;
}
console.log(getNewArray(obj));
边栏推荐
猜你喜欢

ArcMap map map projection related operations

You must configure either the server or JDBC driver (via the ‘serverTimezone)

蓝桥代码 翻硬币(我这样写也通过了,官网测试是不是有问题)

Canvas绘图2

顺序表oj题目

Model builder of ArcGIS

Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later

Microsoft Edge浏览器插件(1)

Review of metallurgical physical chemistry -- Fundamentals of metallurgical reaction kinetics and characteristics of multiphase reaction kinetics

函数基础知识以及特殊点
随机推荐
shell运行原理
On a wonderful journey
c语言:通过一个例子来认识函数栈帧的创建和销毁讲解
Merge two ordered arrays of order table OJ
截图传入后台
JS字符串方法大全
日期类及其基本功能的实现
结果填空 奖券数目(dfs * 数学公式)
三大缓存技术--localStorage、sessionStorage、Cookie
C语言回顾(可变参数篇)
Annotation and grid addition of ArcGIS map making
Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later
使用sourcetree推送仓库时 Failed to connect to www.google.com port 80: Timed out
结果填空 第39级台阶(递归*C语言)
ArcMap map map projection related operations
标准C语言总结1
冶金物理化学复习 --- 化学反应动力学基础
Zotero - a document management tool
在线词云图生成(以WordArt为例)
Solve the problem that Oracle cannot use more than 1000 in statements