Is it better to run for after filter than to run for directly?

one is to filter first, and then run forEach or for to do things

function test(a) {
    this.test.filter(function (i) {
        return a.id == i.id;
    }).forEach(function (item) {
        a.isOpen = !a.isOpen;
    });
}

one is to deal with forEach or for directly

function test(c){
    for (let i in this.test)
        if(test[i].id == c.id)
            test[i].isOpen = !cards[i].isOpen
}


The

code is for reference only. What I want to know is that when there is a lot of data, filter filters out what you want before running for, is it better than running for directly?

May.20,2022

this code is certainly efficient in a traversal, but when the amount of data is small, this efficiency improvement is far less than the fluency of the code and readable


logic is not complex, a for can solve must be a good for!


in general, the number of cycles is the less the better . filter is also essentially filtered through the for loop and then returned, so the first advantage is that simplifies writing , but loops two rounds of at the same time. The second judgment and assignment A for loop is solved.

which one to use depends on your requirements

when the amount of data is small , filter is written. simplifies the code and looks more intuitive. is more readable , but is very inefficient when the amount of data is very large . At the same time, it is not compatible with the lower version of ie
. The second browser supports it. When the logic is complex, the readability of the code will be poor , but when the amount of data is large, the efficiency is much higher than that of filter and forEach .

but usually the amount of data in the daily operation in the scenario can not be very large, and the need for the front end to deal with a very large amount of data is also unreasonable. How to choose or not depends on the development environment


after using a for
filter, not all data is returned.
you can use map or reduce instead of

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37366-34426.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37366-34426.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?