映射数组的道具对象减少(总和)对象中的值在React
问题描述:
我有多个道具对象并将它们传递给一个数组。每个对象包含点值。我试图总结对象中的所有值,并将结果传递到Semantic-ui-react Statistics组件。映射数组的道具对象减少(总和)对象中的值在React
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()
总结所有的值。结果显示像
所以我加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"} />
是否有正常工作一个解决方案?谢谢。
答
你减少功能有几件事情:
-
reduce
应只分析当前值 -
parseInt
应该通过基地为第二个参数去强制小数解析 -
reduce
应该通过在初始值作为第二PARAM
-
const withoutSpousePointSum =
withoutSpousePointsArray
.map(points => points.point)
.reduce((acc, curr) => acc + parseInt(curr, 10), 0);
+0
这项工作就像一个魅力。另外,感谢您的解释。解析只有当前值。很高兴知道。 – MachoBoy
在开始映射过程时,您是否确保'withoutSpousePointsArray'被正确填充? – xDreamCoding
@xDreamCoding所有需要的对象都在数组中,并尝试用'push()'将对象添加到数组中。结果是一样的。所以你的意思是通过将对象添加到数组过程中是不正确的? – MachoBoy