Search code examples
javascriptjavascript-objectses6-promise

How can I store and filter large JSON objects (above 70,000)?


const users = [
    {id:1, email:"abc@email.com"},
    {id:2, email:"xyz@email.com"},
    {....}(~70,000 objects)
]

function a(){
    const id = 545
    users.filter((value)=>{
        if(value.id === id)
            return true
    })
}

We have 70,000 users' objects. we need to filter the email based on the id.

users= [{id: '1001', email: "abc@gmail.com"}, {{id: '1002', email: "spc@gmail.com"} , ..];

Using array and array.filter() ends up in the error. Error

what's the best way of approach for this?


Solution

  • It might be best to convert your array into a Map so that lookups can be made without scanning the entire array. As such:

    const lookupMap = new Map(users.map((u) => [u.id, u]));
    

    so now you can

    const user = lookupMap.get(userId)
    

    without having to scan all 70000 user objects.