【发布时间】: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()
}
})}
)
【问题讨论】:
-
Array.map()将始终生成与输入数组长度相同的新数组(它一对一映射)。如果您想根据某些条件减小数组的大小,可以使用Array.filter()。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript reactjs object mapping