简体   繁体   English

多次执行函数,顺序替换部分参数

[英]Execute function multiple times replacing part of argument sequentially

I have a method (exampleObj.method) that takes arguments of an object and a function. 我有一个方法(exampleObj.method),它接受对象和函数的参数。 This is shown below: 如下所示:

exampleObj.method({
        name1: 'string1',
        name2: 'string2',
        name3: 'string3',
        name4: {
            array1: ['item1a', 'item1b', 'item1c'],
            array2: ['item2a']
        },
        name5: exampleObj.method2
    },
    function exampleFunc(arg1, arg2) {
        // function logic here
    }
};

What I want to achieve is to execute the method multiple times, each time replacing array1 with a different array from the arrayCollection variable in sequence. 我要实现的是多次执行该方法,每次用与arrayCollection变量不同的数组依次替换array1。

var arrayCollection = [
    ['item1d', 'item1e', 'item1f'],
    ['item1g', 'item1h', 'item1i'],
    ['item1j', 'item1k', 'item1l']
];

I know that I could iterate through the arrayCollection array executing the method each time or perhaps use forEach to achieve the same. 我知道我可以遍历每次执行该方法的arrayCollection数组,或者也许使用forEach实现相同的目的。 What I am struggling with is replacing the value of array1 each time the method is executed. 我正在努力的是每次执行该方法时都要替换array1的值。

When I've searched for an answer it seems as if array.map may be an option. 当我搜索答案时,似乎可以选择array.map。 I'm not sure how to use this to change the specific array1 value though. 我不确定如何使用它来更改特定的array1值。

Any suggestions would be much appreciated! 我们欢迎所有的建议!

for(const array1 of arrayCollection)
  exampleObj.method({
        name1: 'string1',
        name2: 'string2',
        name3: 'string3',
        name4: {
            array1,
            array2: ['item2a']
        },
        name5: exampleObj.method2
    },
    function exampleFunc(arg1, arg2) {
        // function logic here
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM