Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vuex状态数组键_Javascript_Arrays_Vuejs2_Javascript Objects_Vuex - Fatal编程技术网

Javascript Vuex状态数组键

Javascript Vuex状态数组键,javascript,arrays,vuejs2,javascript-objects,vuex,Javascript,Arrays,Vuejs2,Javascript Objects,Vuex,我初始化状态 state = { matches: [] } mutations = { setMatches(state,payload) { state.matches = payload } } 在我的getters中,我设置获取密钥 userMatchesId: state => { return [...state.matches.keys()] } 我得到了错误 state.matches.keys is not a function

我初始化状态

state = {
   matches: []
}

mutations = {
   setMatches(state,payload) {
     state.matches = payload
   }
}
在我的getters中,我设置获取密钥

userMatchesId:  state => {
   return [...state.matches.keys()]
}
我得到了错误

state.matches.keys is not a function
我的数据是这样的

{
  "44033":{
    "comments":[
        {
          "comment":"selected Hasan Ali was Bowler and Took a wicket",
          "playerId":9598
        },
        {
          "comment":"selected Fakhar Zaman was Fielder and Caught a catch",
          "playerId":8192
        },
        {
          "comment":"selected Adil Rashid was Bowler and Took a wicket",
          "playerId":3899
        },
        {
          "comment":"selected Ben Stokes was Fielder and Caught a catch",
          "playerId":4489
        },
        {
          "comment":"selected Ben Stokes was Fielder and Caught a catch",
          "playerId":4489
        },
        {
          "comment":"selected \"Ben Stokes\" was and Captain and awarded 2x points",
          "playerId":4489,
          "wasCaptain":true
        }
    ],
    "score":150
  },
  "44034":{
    "comments":[
        {
          "comment":"selected Babar Azam was Fielder and Caught a catch",
          "playerId":5601
        },
        {
          "comment":"selected \"Ben Stokes\" was and Captain and awarded 2x points",
          "playerId":4489,
          "wasCaptain":true
        }
    ],
    "score":10
  }
}
我需要创建两个数组,一个包含这个数组的键,另一个包含score的数组

我曾尝试在vue组件中使用计算属性映射返回,但我想我遗漏了一些东西

我也试过了

  let score;
  state.matches = [...Array.from(payload.matches).keys()];

  Array.from(payload.matches).forEach(match => {
    match.score += score
  });

  state.score = score;
我想要的结果是

matches: ["44033","44044"]
score: ["150",10]

为了实现您想要的功能,您必须使用2个功能:

Object.keys,它将返回对象的键数组 大堆​.原型​.将返回所提供函数的结果数组的映射 常数数据={ 44033: { 分数:150 }, 44034: { 分数:10 } } const id=Object.keysdata/[4403344034] console.logids //对于ids中的每个id,返回数据[id]。分数 const scores=ids.mapid=>data[id].scores/[150,10]
console.logscoresLooks这样的不是vuejs或vuex问题,而是。您能否编辑您的问题,向我们展示阵列的外观?您想做什么有点不清楚。@Stilleur编辑了这个问题以显示结果Wesome thank you:那是因为您在spread运算符的末尾有一个值,它断言您的键是一个方法。试试看,不要吃