简体   繁体   中英

Execute function multiple times replacing part of argument sequentially

I have a method (exampleObj.method) that takes arguments of an object and a function. 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.

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. What I am struggling with is replacing the value of array1 each time the method is executed.

When I've searched for an answer it seems as if array.map may be an option. I'm not sure how to use this to change the specific array1 value though.

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
    }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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