【发布时间】:2017-02-05 22:15:41
【问题描述】:
好的,这是我的泡菜,我在actions/index.js 中的一个操作是:
export function requestPeople() {
return (dispatch, getState) => {
dispatch({
type: REQUEST_PEOPLE,
})
const persistedState = loadState() // just loading from localStorage for now
console.log(persistedState)
//Object.keys(persistedState).forEach(function(i) {
//var attribute = i.getAttribute('id')
//console.log('test', i,': ', persistedState[i])
//myArr.push(persistedState[i])
//})
//console.log(myArr)
//dispatch(receivePeople(persistedState)) // I'm stuck here
}
}
当我在 Chrome 控制台中从上方console.log(persistedState) 时,我的people 状态与此完全相同。
Object {people: Object}
然后当我深入到上面的people: Object 时,我会得到这样的结果:
abc123: Object
abc124: Object
abc125: Object
当我深入研究每只小狗时(通过点击 Chrome 控制台中的小三角形),我得到的每只小狗都是这样的:
abc123: Object
firstName: 'Gus'
id: 'abc123'
lastName: 'McCrae'
// when I drill down into the second one I get
abc124: Object
firstName: 'Woodrow'
id: 'abc124'
lastName: 'Call'
现在,这就是我卡住的地方。
我使用的表是 Allen Fang 的 react-bootstrap-table,它只接受数组,它被称为 <BootstrapTable data={people} /> 所以我上面的数据需要转换成这样的数组:
const people = [{
id: abc123,
firstName: 'Gus',
lastName: 'McCrae'
}, {
id: abc124,
firstName: 'Woodrow',
lastName: 'Call'
}, {
...
}]
// and eventually I'll call <BootstrapTable data={people} />
我的具体问题是如何将上面显示的people 状态转换为这个必要的数组?在我尝试过的action/index.js 文件中:Object.keys(everything!!!)
最后,一旦我有了数组,使用状态、变量、分派动作(我从未听说过)将该数组传递给 <BootstrapTable data={here} /> 的最佳方法是什么?
任何帮助将不胜感激!仅供参考,这是我在 Stack Overflow 中的第一个问题,感觉很怀旧。我是一名全职警官,并试图在一边学习编码。再次感谢!
更新:
多亏了 Piotr Berebecki 的建议,我才把它绑起来:
export function requestPeople() {
return (dispatch, getState) => {
dispatch({
type: REQUEST_PEOPLE,
})
const persistedState = loadState()
console.log('persistedState:', persistedState)
const peopleArr = Object.keys(persistedState.people).map(function(key) {
return persistedState[key]
})
console.log(JSON.stringify(peopleArr))
//dispatch(receivePeople(persistedState))
}
}
得到 [null,null,null]
像这样:
【问题讨论】:
标签: javascript arrays reactjs redux react-bootstrap-table