简体   繁体   English

有人能告诉我如何只对嵌套在我的 JavaScript 数组中的一些项目求和吗?

[英]Can someone tell me how to sum only some of items nested in my JavaScript array?

I am not very proud of this codeㅠㅠ I have 6000 objects in the array and would like to sum only 10 items from the array.我对这个代码不是很自豪ㅠㅠ 我在数组中有 6000 个对象,并且只想对数组中的 10 个项目求和。 Now I am putting ten coding blocks to the program.现在我将十个编码块放入程序中。

Someone can tell me how to make it look like programing?有人可以告诉我如何使它看起来像编程吗?

<Text style={styles.text}>[{Number((items.find( x => x.id === indexTwoId+101).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+102).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+103).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+104).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+105).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+106).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+107).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+108).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+109).as==='1'?'1':'0'))+Number((items.find( x => x.id === indexTwoId+110).as==='1'?'1':'0'))}/10]</Text>

My array is as follows;我的数组如下;

    count: 1,
    categories: [
        {id:1,as:0},
        {id:2,as:0},
        {id:3,as:0},
        {id:4,as:0},
        {id:5,as:0},
        {id:6,as:0},
        {id:7,as:0},
        {id:8,as:0},
        {id:9,as:0},
        {id:10,as:0},
.
.
.
.]}```

I don't have experience with React, but in "regular" JS you might do something like this:我没有使用 React 的经验,但在“常规”JS 中你可能会做这样的事情:

let sum = items.reduce((item, total) => {
    if (item.id < indexTwoId + 101 || item.id > indexTwoId + 110) return total;
    if (item.as === '1') return total + 1;
    return total;
}, 0)

You can probably "translate" it to a single line with ternary operators.您可能可以使用三元运算符将其“翻译”为一行。

EDIT: If you just want to count items by criteria then:编辑:如果您只想按标准计算项目,则:

items.filter(x => x.id >= indexTwoId + 101 && x.id <= indexTwoId + 110 && x.as === '1').length

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我如何用循环替换以下javascript代码吗? - Can someone tell me how to replace the following javascript code with a loop? 有人可以告诉我如何删除或隐藏此 JavaScript 代码吗? - Can someone please tell me how to remove or hide this JavaScript code? 有人可以告诉我这段代码如何运作吗? - Can someone tell me how this code works? 有人可以用这个 javascript 正则表达式帮助我将一串列表项转换为仅包含项目文本的数组(没有列表项 # 或换行) - Can someone help me with this javascript regex to turn a string of list items into an array with the item texts only (no list item # or new line) 你能告诉我为什么我的将数组转换为数字、加一和返回数组的代码只适用于某些数据集吗? - Can you tell me why my code for turning an array to a number, adding one, and returning an array only works on some datasets? JavaScript:有人可以告诉我我的代码出了什么问题吗? - JavaScript: Can someone tell me what's going wrong with my code? 有人可以告诉我我的下拉菜单javascript代码有什么问题吗 - Can someone tell me what's wrong with my dropdown menu javascript code 谁能告诉我为什么这个JavaScript代码没有按顺序排列一个数组? - Can someone tell me why this JavaScript code isn't lining up an array in order? 有人请告诉我$ .parseJSON有什么问题[javascript / jquery] - Someone please tell me what is wrong with my $.parseJSON [javascript / jquery] 如何用javascript编写PDF并从服务器中下载pdf ...有人可以告诉我如何解决这个问题 - How to write into PDF and download pdf from server in javascript… can someone tell me how to solve this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM