如何将Redux存储的一部分转换为数组

时间:2016-09-28 05:46:04

标签: javascript arrays reactjs redux react-bootstrap-table

好的,这是我所处的泡菜,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]

像这样:

enter image description here

1 个答案:

答案 0 :(得分:2)

欢迎使用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"}
]
*/