映射道具对象数组减少(求和)React中对象的值

时间:2017-10-09 23:33:01

标签: reactjs reduce semantic-ui-react array.prototype.map

我有多个道具对象并将它们传递给一个数组。 每个对象包含点值。我试图对对象中的所有值求和,并将结果传递给Semantic-ui-react Statistics组件。

const withoutSpousePointsArray = [
            withoutSpouseAgePoint, 
            withoutSpouseLoePoint, 
            withoutSpouseOlpPoint, 
            withoutSpouseOlp2Point, 
            withoutSpouseExpPoint, 
            BwithSpouseLoePoint,
            BwithSpouseOlpPoint,
            BwithSpouseExpPoint,
            skillEducation1Point,
            skillEducation2Point,
            skillFwe1Point,
            skillFwe2Point,
            skillCertificatePoint,
            additionalPoint,
];
const mapStateToProps = state => ({
    // core withSpouse states
    withSpouseAgePoint: state.withSpouseAge,
    withSpouseLoePoint: state.withSpouseLoe,
    withSpouseOlpPoint: state.withSpouseOlp,
    withSpouseOlp2Point: state.withSpouseOlp2,
    withSpouseExpPoint: state.withSpouseExp,
    // core withoutSpouse
    withoutSpouseAgePoint: state.withoutSpouseAge,
    withoutSpouseLoePoint: state.withoutSpouseLoe,
    withoutSpouseOlpPoint: state.withoutSpouseOlp,
    withoutSpouseOlp2Point: state.withoutSpouseOlp2,
    withoutSpouseExpPoint: state.withoutSpouseExp,
    // Spouse factor
    BwithSpouseLoePoint: state.BwithSpouseLoe,
    BwithSpouseOlpPoint: state.BwithSpouseOlp,
    BwithSpouseExpPoint: state.BwithSpouseExp,
    // skill transferability factors
    skillEducation1Point: state.skillEducation1,
    skillEducation2Point: state.skillEducation2,
    skillFwe1Point: state.skillFwe1,
    skillFwe2Point: state.skillFwe2,
    skillCertificatePoint: state.skillCertificate,
    // Additional
    additionalPoint: state.additional,
});

由于我有.map()个多个对象,因此点值首先,.reduce()对所有值求和。结果显示为this

所以我添加parsInt()默认数字设置为0但是当我发送动作时,它再次向我显示了很多0并且数字没有正确加起来。

const withoutSpousePointSum = 
        withoutSpousePointsArray.map(points => points.point)
        .reduce((acc, curr) => [parseInt(acc + curr)]);

<Points key={shortid.generate()} color={"orange"} value={withoutSpousePointSum} label={"Points"} />

有没有正确的解决方案?谢谢。

1 个答案:

答案 0 :(得分:0)

关于reduce功能的一些事项:

  • public async Task SyncCoffee() { try { await coffeeTable.PullAsync("allusers", coffeeTable.CreateQuery()); await MobileService.SyncContext.PushAsync(); } catch (Exception ex) { Debug.WriteLine("Unable to sync coffees, that is alright as we have offline capabilities: " + ex); } } 应仅解析当前值
  • reduce应该将基数作为第二个参数传递给强制小数解析
  • parseInt应该将初始值作为第二个参数传递

-

reduce