【问题标题】:How to get the array object from a specific history.location.pathname如何从特定的 history.location.pathname 获取数组对象
【发布时间】:2021-07-15 00:30:35
【问题描述】:

我的问题是,当我调用函数 getAllFlashCardsFromQuest() 时,它会调用所有页面中创建的所有闪存卡,我只想要来自特定路径名的对象,或者过滤卡片数组的方法。

async function getCards() {
    let cardsValues = await getAllFlashCardsFromQuest() as FlashCard[]
    let cardsFiltered = cardsValues.filter(()=>{
        return history.location.pathname === 'CriarAlternativaQuest'
    })
    console.log(cardsFiltered)
    setCards(cardsFiltered)
}

对象如下所示:

【问题讨论】:

    标签: reactjs location history pathname


    【解决方案1】:

    您的代码没有太多上下文,所以我只能提供这么多帮助,但有一件事是肯定的,那就是 history.location.pathname 没有改变,您正在将它与另一个常量进行比较。所以cardsFiltered 将是 true 列表或 false 列表。

    每当您过滤列表时,您需要获取每个项目或每个项目的属性并在比较中使用它。

    例如

    async function getCards() {
        let cardsValues = await getAllFlashCardsFromQuest() as FlashCard[]
        let cardsFiltered = cardsValues.filter((cardValueItem) => {
            return cardValueItem.pathname === 'CriarAlternativaQuest'
        })
    }
    

    问题是您需要弄清楚您需要将 cardsValues 列表中的哪些值或属性与“CriarAlternativaQuest”进行比较

    【讨论】:

    • 我知道,但是没有其他属性与我可以比较的cardsValues不同,因为对象相同,我想按对象的来源进行过滤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多