How to manipulate js arrays quickly and efficiently?

the array operation of js has always been a headache for me. In the past, dealing with arrays is a variety of foreach nesting, filter,map. After writing, I always feel that there will be some optimization, but I really don"t know how to optimize it because of my limited ability. The code is as follows:

let arr = [{
        id: 10,
        name: "IOS",
        status: false
    }, {
        id: 11,
        name: "Android",
        status: false
    },
    {
        id: 12,
        name: "",
        status: false
    },
    {
        id: 13,
        name: "",
        status: false
    },
    {
        id: 20,
        name: "PC",
        status: true
    }]
    // statustrueid
    
    let arr2 = arr.filter(item => item.status)
    let arr3 = arr2.map(item => item.id)
    let str = arr3.join(",")

ask all the bosses for advice.

Dec.24,2021

semantically, it is more recommended to use your way of writing, which is simple and intuitive. In fact, your writing method can be simplified to

.
 let str = arr.filter(item => item.status).map(item => item.id).join(',');

however, this method of writing has been cycled twice. In pursuit of efficiency, you can try to change the following method of writing only once in a loop, but the code is difficult to understand

.
let str = arr.reduce((_arr, item) => item.status && _arr.push(item.id) ? _arr : _arr, []).join(',');

if you optimize manually, you can merge the loop.
you can also rely on the lazy evaluation feature of lodash to let lodash optimize it automatically

let str=_.chain(arr).filter('status').map('id').join(',').value()
let arr = [];
for (let i = 1; i < 10000000; iPP) {
    arr.push({id: i, status: Math.random() > 0.5});
}

console.time('time1');
let arr2 = arr.filter(item => item.status);
let arr3 = arr2.map(item => item.id);
var str = arr3.join(',');
console.timeEnd('time1');

const _ = require('lodash');
const calculateChain = _.chain(arr).filter('status').map('id').join(',');
console.time('time2');
var str = calculateChain.value();
console.timeEnd('time2');

console.time('time3');
var str = arr.reduce((_arr, item) => item.status && _arr.push(item.id) ? _arr : _arr, []).join(',');
console.timeEnd('time3');


it is the fastest to use lodash in actual measurement. It should be that join is also an o (n) operation, and there is room for optimization

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-1b3aee3-2c24c.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-1b3aee3-2c24c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?