Javascript 将Firebase元素映射为数组

Javascript 将Firebase元素映射为数组,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我正在开发一个池投注应用程序。一切看起来都很好,我正在完成它的最后一个模块 在firebase中,我有一个赌注池,有很多用户。任何用户都有许多比赛,他们在比赛中下注。像这样: fetchPoolData = async () => { const { firebaseApp } = this.props; await firebaseApp .database() .ref(`/pools/${this.props.location.state.

我正在开发一个池投注应用程序。一切看起来都很好,我正在完成它的最后一个模块

在firebase中,我有一个赌注池,有很多用户。任何用户都有许多比赛,他们在比赛中下注。像这样:

 fetchPoolData = async () => {
    const { firebaseApp } = this.props;

    await firebaseApp
      .database()
      .ref(`/pools/${this.props.location.state.pool.key}/users`)
      .once("value")
      .then(snapshot => {
        this.setState({
          poolData: this.snapshotToArray(snapshot),
          isLoadingPool: false
        });
      });
  };
this.props.poolData.map(user => {
  allUserMatches.push(calculatePoints(user.matches, outcomeMatches));
});
Object.keys(user.matches)
(66) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "result", "topscorer"

当我在firebase上执行以下请求时:

 fetchPoolData = async () => {
    const { firebaseApp } = this.props;

    await firebaseApp
      .database()
      .ref(`/pools/${this.props.location.state.pool.key}/users`)
      .once("value")
      .then(snapshot => {
        this.setState({
          poolData: this.snapshotToArray(snapshot),
          isLoadingPool: false
        });
      });
  };
this.props.poolData.map(user => {
  allUserMatches.push(calculatePoints(user.matches, outcomeMatches));
});
Object.keys(user.matches)
(66) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "result", "topscorer"
一切看起来都很好。现在我有一个用户数组,我想进行一些计算,以获得每个用户在每次比赛中的得分。因此,我会像这样映射用户:

 fetchPoolData = async () => {
    const { firebaseApp } = this.props;

    await firebaseApp
      .database()
      .ref(`/pools/${this.props.location.state.pool.key}/users`)
      .once("value")
      .then(snapshot => {
        this.setState({
          poolData: this.snapshotToArray(snapshot),
          isLoadingPool: false
        });
      });
  };
this.props.poolData.map(user => {
  allUserMatches.push(calculatePoints(user.matches, outcomeMatches));
});
Object.keys(user.matches)
(66) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "result", "topscorer"
这里的问题是user.matches没有作为数组进行检索。相反,它就像一个大的json对象,我可以映射它并用我的数学来计算点

如何映射此元素?如果我做Object.keys,我只得到这样一个对象:

 fetchPoolData = async () => {
    const { firebaseApp } = this.props;

    await firebaseApp
      .database()
      .ref(`/pools/${this.props.location.state.pool.key}/users`)
      .once("value")
      .then(snapshot => {
        this.setState({
          poolData: this.snapshotToArray(snapshot),
          isLoadingPool: false
        });
      });
  };
this.props.poolData.map(user => {
  allUserMatches.push(calculatePoints(user.matches, outcomeMatches));
});
Object.keys(user.matches)
(66) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "result", "topscorer"
如果我做一个Object.entries,它仍然不是很有用(因为我需要在数组的对象上做一个查找,以获得由对象的atribute过滤的元素)


我在这里迷路了。怎么办

如果您想使用这种映射,可以使用

Object.keys(user.matches).map(key => user.matches[key])
上面将返回每个对象的关键点,稍后可以通过它们的关键点在对象的地图中使用这些关键点。 另外,它不会以数组的形式返回,因为您有两个与数组索引不同的野生属性(result和topcorer),所以它需要将它们作为对象而不是数组返回

一种从该键值对中检索所有值而不使用result和topcorer的方法

Object.keys(user.matches).filter(key => Number.isInteger(parseInt(key))).map(key => user.matches[key]). 

这将使用数字索引(类型字符串)将对象转换为所需的数组:

Object.keys(user.matches).reduce((arr, key) => {
  arr[parseInt(key)] = user.matches[key]
  return arr
}, [])

我想这更多的是关于将具有数字索引的对象转换为数组。您希望数据是什么样子的?如下:数组(66)0:{away_结果:“1”,away_团队:2,日期:“2018-06-14T18:00:00+03:00”,完成:false,组:“a”,…}1:{away_结果:“1”,away_团队:4,日期:“2018-06-15T17:00+05:00”,完成:false,组:“a”,…}2:{away_结果:“2”,away_团队:6,日期:“2018-06-15T21:00+03:00”是的,但是你想把它转换成什么样子呢?我想忘记firebase对我的物体所做的这些奇特的键。我想要一个好的旧数组,从0开始到66结束,这个数组的每个值都类似于“客场结果:”1,客场团队:2,日期:“2018-06-14T18:00:00+03:00”,完成:false“我没有得到它Siroo”。我在这里做的是,我的user.matches变成了这样一个数组:0:away\u结果:“1”away\u团队:2日期:“2018-06-14T18:00:00+03:00”finished:false组:“a”home\u结果:“1”home\u团队:1键:“1”name:1体育场:1类型:“group”原型:对象1:away\u结果:“1”away\u团队:4日期:“2018-06-15T17:00:00+05:00”这两个通配符属性没有问题,因为在这个值中,我有一个可以正确筛选的属性。但在该user.matches中也有两个看起来不像数组索引的属性(“result”,“topcorer”)因此Firebase将其作为对象字段返回,数组索引作为属性名,并返回结果和topscorer。如果您想删除这两个字段,还可以执行类似Object.keys(user.matches).filter(key=>Number.isInteger(parseInt(key))).map(key=>user.matches[key])的操作。在上面这一行中,你将有数组元素的正常映射迭代,排除结果和TopCoreral。因此,我的建议是避免出现这样的情况:你想要一个数组,但无意中你添加了两个打破该结构的proeprities,这样Firebase会自动将其转换为对象键值对,而不是带有索引的数组。这样处理数据就容易多了,忘掉它吧。你告诉我的那个选择很有效。谢谢你,伙计!