【问题标题】:How not to map an object through the entire response如何不在整个响应中映射对象
【发布时间】:2020-03-08 16:22:15
【问题描述】:

我目前有一个表格,其中包含来自 axios 调用的 3 个不同条目。我正在使用const details 对象映射该响应以测试我的响应中的数据。我成功地为所有 3 个响应映射了我的 const details 对象。但我只想让我的详细信息对象显示 表的 2/3 条目,而不是完整的 3. 不让我的对象映射到最终条目的最佳方法是什么?

const details = 
        {
            ActionStatus: "Pending",
            RequestedBy: "Jon Snow",
            ActionRequested: "Canceled"
        },

    const getInfo= () => {
        return infoApi
            .getDirectInfo({
                date: selectedDate,
            })
            .then(response => {
                console.log(response)
                dispatch({
                    type: "FETCH_INFO",
                    payload: {
                        loading: false,
                        data: response.map(r=>({...r,...details})),
                        lastUpdated: new Date().getTime()
                    }
                })}
            )

【问题讨论】:

标签: javascript reactjs object mapping


【解决方案1】:

Array.map() 将始终生成与输入数组长度相同的新数组(它一对一映射)。如果您想根据某些条件减小数组的大小,可以使用Array.filter()https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 2019-09-24
    • 2021-12-03
    • 2011-11-25
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多