简体   繁体   中英

Use for-of to assign to the elements of an array

Is there any way to use a for-of loop to assign to elements of an iterable? Something like this, where it actually will change the values of the elements in the array.

for (let e of some_array) {
    e = new_value;
}

You can use Array#entries() to get the array element and the index:

for (let [i, e] of some_array.entries()) {
    some_array[i] = new_value;
}

But in such a case I would probably just stick with a normal for loop or use Array#map .

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