如何将部分 Redux 存储转换为数组
How do I turn part of Redux store into an array
好吧,这是我的泡菜,我在 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'
现在,这就是我被困的地方。
我用的 table 是 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]
像这样:
欢迎来到 Stack Overflow :)
要将嵌套的 persistedState.people
对象转换为数组,您可以先使用 Object.keys(persistedState.people)
建立一个临时键数组,然后 map()
在键上用对象替换每个键在您的原始嵌套对象中找到 - persistedState.people
- 在该键处。您可以将结果数组分配给一个变量,然后您可以将该变量传递给 BootstrapTable
。查看下面的代码和此处的演示:http://codepen.io/PiotrBerebecki/pen/yaXrVJ
const persistedState = {
people: {
'abc123' : {
id:'abc123',firstName: 'Gus', lastName: 'McCrae'
},
'abc124' : {
id:'abc124',firstName: 'Woodrow', lastName: 'Call'
},
'abc125' : {
id:'abc125',firstName: 'Jake', lastName: 'Spoon'
}
}
}
const peopleArr = Object.keys(persistedState.people).map(function(key) {
return persistedState.people[key];
});
console.log(JSON.stringify(peopleArr));
/*
Logs the following array:
[
{"id":"abc123","firstName":"Gus","lastName":"McCrae"},
{"id":"abc124","firstName":"Woodrow","lastName":"Call"},
{"id":"abc125","firstName":"Jake","lastName":"Spoon"}
]
*/
好吧,这是我的泡菜,我在 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'
现在,这就是我被困的地方。
我用的 table 是 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]
像这样:
欢迎来到 Stack Overflow :)
要将嵌套的 persistedState.people
对象转换为数组,您可以先使用 Object.keys(persistedState.people)
建立一个临时键数组,然后 map()
在键上用对象替换每个键在您的原始嵌套对象中找到 - persistedState.people
- 在该键处。您可以将结果数组分配给一个变量,然后您可以将该变量传递给 BootstrapTable
。查看下面的代码和此处的演示:http://codepen.io/PiotrBerebecki/pen/yaXrVJ
const persistedState = {
people: {
'abc123' : {
id:'abc123',firstName: 'Gus', lastName: 'McCrae'
},
'abc124' : {
id:'abc124',firstName: 'Woodrow', lastName: 'Call'
},
'abc125' : {
id:'abc125',firstName: 'Jake', lastName: 'Spoon'
}
}
}
const peopleArr = Object.keys(persistedState.people).map(function(key) {
return persistedState.people[key];
});
console.log(JSON.stringify(peopleArr));
/*
Logs the following array:
[
{"id":"abc123","firstName":"Gus","lastName":"McCrae"},
{"id":"abc124","firstName":"Woodrow","lastName":"Call"},
{"id":"abc125","firstName":"Jake","lastName":"Spoon"}
]
*/