我想获得等于一定数量的所有值并计算每个对象中的多少个。
我的代码看起来像这样:
var countItems = {
"aa":"70",
"bb":"70",
"cc":"80",
"dd":"90",
"ee":"90",
"ff":"90"
}
现在我想做的就是在下半场计数每个。
例如,有两个“ 70”,一个“ 80”和三个90
var firstCounter = ?? // 2
var secondCounter = ?? // 1
var thirdCounter = ?? // 3
。
如果像以下内容一样构成不同的构造,我可以这样做:
let firstCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '70') firstCounter++;
}
let secondCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '80') secondCounter++;
}
let thirdCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '90') thirdCounter++;
}
但是,我的原始代码不是那样的结构,所以我不确定如何调整它。
我如何计算原始列表中的项目( var countitems
),以便找出每个值多少?
I want to get all the values that equal a certain number and count how many of each of the objects.
My code looks like this:
var countItems = {
"aa":"70",
"bb":"70",
"cc":"80",
"dd":"90",
"ee":"90",
"ff":"90"
}
Now what I want to do is count each on that is in the second half.
For example, there are two "70", one "80", and three 90. Then I can assign to variables:
var firstCounter = ?? // 2
var secondCounter = ?? // 1
var thirdCounter = ?? // 3
??
is I don't know what goes here.
If it was structed differently like the following, I could do it like this:
let firstCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '70') firstCounter++;
}
let secondCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '80') secondCounter++;
}
let thirdCounter = 0;
for (let i = 0; i < countItems.length; i++) {
if (countItems[i].status === '90') thirdCounter++;
}
But the thing is, my original code which is what I have is not structured like that, so I'm not sure how to adapt it.
How can I count the items in the original list (var countItems
) so that I can find out how much each value is?
发布评论
评论(6)
您可以使用
object.values(countitems)
获得看起来像这样的数组:[“ 70”,“ 70”,“ 80”,“ 90”,“ 90”,“ 90”,“ 90“]
然后,要么使用循环的有条件地增加所需的任何计数器,要么使用
array.reduce.reduce
或array.filter.filter.filter
计算所需的元素。You could use
Object.values(countItems)
to get an array that looks like this:["70","70","80","90","90","90"]
then either use afor
loop to conditionally increment whatever counters you want, or use something likeArray.reduce
orArray.filter
to count the elements you need.您可以使用
降低
来创建一个计数的哈希地图,因此:You could use
reduce
to create a counted hash map like so:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
您可以访问这样的对象键:
您也可以在对象上循环(如果您想像示例中这样做一样):
You can access object keys like this :
You can also loop on the object (if you want to do as you did in your example) :
object.values()
和redable()
都是正确的想法。在一起...Object.values()
andreduce()
are both the right ideas. Taken together...名称阵列上的reald()方法计算每个名称的出现并将结果存储在countednames对象中。让我们逐步浏览代码以了解其工作原理:
const names = [“爱丽丝”,“鲍勃”,“ tiff”,“ bruce”,“ alice”];
在这里,我们初始化了一个名为名称的数组,其中包含多个名称,包括重复条目。
const countednames = names.Reduce((AllNames,name)=&gt; {...},{});
我们声明一个可变计数名称,并将其分配为应用于名称数组的redair()方法的结果。 RELAD()方法会在数组的每个元素上迭代,并根据回调函数累积单个值。
(AllNames,name)=&gt; {...}
这是提供给Reled()方法的回调函数。它需要两个参数:AllNames和名称。 AllNames表示将保留每个名称计数的累积对象,名称表示正在处理的当前元素。
const currcount = allnames [name] ?? 0;
在回调函数中,我们从Allnames对象中检索名称的当前计数。如果该名称在AllNames对象中不存在,则将CurrCount设置为0。
返回{... AllNames,[name]:CurrCount + 1};
我们返回一个新对象,将现有的AllNames对象与一个新属性[名称]相结合,其值为CurrCount + 1。这将当前名称的计数增量为1。
{}(for Realding()初始值())
{}作为第二个参数()传递为AllNames参数的初始值。它表示一个空的对象,该对象将存储每个名称的计数。
RELAD()方法在名称数组中的每个名称上迭代,对于每个名称,它检查它是否已经存在于AllNames对象中。如果存在,它将计数增加1;否则,它将名称添加为一个新属性,计数为1。此过程累积了每个名称的计数,从而导致最终计数名称对象:
The reduce() method on the names array to count the occurrences of each name and store the results in the countedNames object. Let's go through the code step by step to understand how it works:
const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];
Here, we initialize an array called names containing several names, including duplicate entries.
const countedNames = names.reduce((allNames, name) => {...}, {});
We declare a variable countedNames and assign it the result of the reduce() method applied to the names array. The reduce() method iterates over each element of the array and accumulates a single value based on a callback function.
(allNames, name) => {...}
This is the callback function provided to the reduce() method. It takes two parameters: allNames and name. allNames represents the accumulated object that will hold the counts of each name, and name represents the current element being processed.
const currCount = allNames[name] ?? 0;
Inside the callback function, we retrieve the current count for the name from the allNames object. If the name doesn't exist in the allNames object, currCount is set to 0.
return {...allNames, [name]: currCount + 1};
We return a new object that combines the existing allNames object with a new property [name] whose value is currCount + 1. This increments the count of the current name by 1.
{} (initial value for reduce())
The {} passed as the second argument to reduce() is the initial value for the allNames parameter. It represents an empty object that will store the counts of each name.
The reduce() method iterates over each name in the names array, and for each name, it checks if it already exists in the allNames object. If it exists, it increments the count by 1; otherwise, it adds the name as a new property with a count of 1. This process accumulates the counts of each name, resulting in the final countedNames object: