Home > Blockchain >  Javascript list filter not work as expect
Javascript list filter not work as expect

Time:12-21

I'm trying to find the object's key that matches a specific value, but I only get an empty array, even though I'm sure that there is an object that matches the value. What am I doing wrong?

const list = this.myList.filter((item) => {
      return item.key === key
    })

here is my sample data:

let list = [{"key":"-NJdLxqKEJpDE_7zkmF9","createTime":1671436164193,"lastUpdate":1671436164193,"roomHost":"[email protected]","roomId":25930,"roomMembers":["[email protected]"],"roomName":"Test","todo":[{"addUser":"[email protected]","lastUpdate":1671436158447,"taskName":"1"},{"addUser":"[email protected]","lastUpdate":1671436159985,"taskName":"2"}]}]

and the key is '-NJdLxqKEJpDE_7zkmF9' I have tried following code, and I still get a empty array. I think the problem might be on the string value?

let list = [{"key":"-NJdLxqKEJpDE_7zkmF9","createTime":1671436164193,"lastUpdate":1671436164193,"roomHost":"[email protected]","roomId":25930,"roomMembers":["[email protected]"],"roomName":"Test","todo":[{"addUser":"[email protected]","lastUpdate":1671436158447,"taskName":"1"},{"addUser":"[email protected]","lastUpdate":1671436159985,"taskName":"2"}]}]

let newList = list.filter((item)=>{
    item.key === '-NJdLxqKEJpDE_7zkmF9'
})

CodePudding user response:

If the JavaScript list filter is not working as expected, there could be a few possible causes:

Syntax errors: Check for any syntax errors in your code, such as missing brackets or semicolons.

Incorrect data type: Make sure that you are passing the correct data type to the filter function. For example, if you are trying to filter an array of numbers but are passing a string, the filter will not work correctly.

Incorrect filter function: Double-check the function that you are using to filter the list. Make sure that it is correctly written and is correctly filtering the list based on your desired criteria.

Incorrect list: Make sure that you are passing the correct list to the filter function. If you are using an incorrect list, the filter will not work as expected.

Incorrectly referencing the list: Make sure that you are correctly referencing the list in your code. If you are using the wrong variable name or are referencing an element of the list incorrectly, the filter will not work correctly.

To troubleshoot the issue, you may want to try using console.log statements to debug your code and see what is happening at each step. You may also want to check the documentation for the filter function to make sure that you are using it correctly.

CodePudding user response:

You do not really need the return statement. You haven't specified exactly what you need to filter out. Here is how you filter

 list = [
       {
         id:1,
         num:20
        },
        {
         id:2,
         num:22
        }
        ]
        

              const l =this.list.filter((item)=> item.id ==2)

      console.log(l)

Here the value of l would be 2

CodePudding user response:

This is going to drive you nuts until you spot the simple mistake because you start with the correct code in the first box.

Your code:

let list = [{"key":"-NJdLxqKEJpDE_7zkmF9","createTime":1671436164193,"lastUpdate":1671436164193,"roomHost":"[email protected]","roomId":25930,"roomMembers":["[email protected]"],"roomName":"Test","todo":[{"addUser":"[email protected]","lastUpdate":1671436158447,"taskName":"1"},{"addUser":"[email protected]","lastUpdate":1671436159985,"taskName":"2"}]}]

let newList = list.filter((item)=>{
    item.key === '-NJdLxqKEJpDE_7zkmF9'
    // return undefined (or filter everything)
})

Return that item.key check:

let list = [{"key":"-NJdLxqKEJpDE_7zkmF9","createTime":1671436164193,"lastUpdate":1671436164193,"roomHost":"[email protected]","roomId":25930,"roomMembers":["[email protected]"],"roomName":"Test","todo":[{"addUser":"[email protected]","lastUpdate":1671436158447,"taskName":"1"},{"addUser":"[email protected]","lastUpdate":1671436159985,"taskName":"2"}]}]

let newList = list.filter((item)=> {
    // this will return true on match and include the item in the result
    return item.key === '-NJdLxqKEJpDE_7zkmF9'
})
  • Related