如何运行数组的功能 [英] How to run a function of arrays

查看:55
本文介绍了如何运行数组的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个阵列和一个功能(我不知道如何访问所有这些以获得该功能的良好输出:



i have got 3 arrays and a function(i dont know how too access all of them to get a fine output for the function:

const input1 = document.getElementById('item1');
const input2 = document.getElementById('item2');
const input3 = document.getElementById('item3');

const data1 = ["1", "2", "3"];
const data2 = ["a", "b", "c"];
const data3 = ["a1", "b2", "c3"];

const maker = (???what write inside???) => {
console.log(data[i] + " + " + data2[i] + " = " + data3[i]);
}

// i dont know how to access all the arrays at once.. :C
data1.forEach(number => {
    maker(number);
});




$如果有人帮我的话,b $ b生病了。

在这里我坚持不懈。



我尝试了什么:



在谷歌上读了一下,但还没有找到指令答案。



ill really appriciate if someone would help me.
here i stucked hard.

What I have tried:

read a bit on the google, but have not found the directive answer.

推荐答案

不完全清楚你想要做什么,但这样的事情会起作用:

Not entirely clear what you're trying to do, but something like this will work:
const maker = i => console.log(data[i], " + ", data2[i], " = ", data3[i]);

data1.forEach((_, index) => maker(index));



<$ c $的第二个参数c> forEach callback是数组中项目的索引。

Array.prototype.forEach() - JavaScript | MDN [ ^ ]



但是,这依赖于具有相同数量项目的所有三个数组。根据您的要求,改为使用单个数组可能更有意义:


The second argument to the forEach callback is the index of the item in the array.
Array.prototype.forEach() - JavaScript | MDN[^]

However, this relies on all three arrays having the same number of items. Depending on your requirements, it might make more sense to have a single array instead:

const data = [
    { op1: "1", op2: "a", result: "a1" },
    { op1: "2", op2: "b", result: "b2" },
    { op1: "3", op2 : "c", result: "c3" }
];

data.forEach(item => console.log(item.op1, " + ", item.op2, " = ", item.result));


这篇关于如何运行数组的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆