-1

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?

Ryan M
  • 15,686
  • 29
  • 53
  • 64
sdsdc1
  • 13
  • 5

5 Answers5

1

You could use Object.values(countItems) to get an array that looks like this: ["70","70","80","90","90","90"] then either use a for loop to conditionally increment whatever counters you want, or use something like Array.reduce or Array.filter to count the elements you need.

ahrampy
  • 121
  • 1
  • 1
  • 7
0

You could use reduce to create a counted hash map like so:

const countItems = [
  { data: 'aa', status: '70' },
  { data: 'bb', status: '70' },
  { data: 'cc', status: '80' },
  { data: 'dd', status: '90' },
  { data: 'ee', status: '90' },
  { data: 'ff', status: '90' },
];

const countedHash = countItems.reduce((acc, curr) => {
  if (!acc[curr.status])
    acc[curr.status] = 1
  else
    acc[curr.status] += 1
  return acc
}, {})

/* print out the results */
console.log(countedHash)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Asleepace
  • 2,966
  • 1
  • 16
  • 29
  • The array you posted an answer to was only an example but it wasn't the code I'm trying to extract from – sdsdc1 May 24 '22 at 23:31
  • @sdsdc1 no worries this will count objects in an array, the only thing you may need to change is the `status` property – Asleepace May 24 '22 at 23:32
0

You can access object keys like this :

countItems["aa"] // it will return "70"

You can also loop on the object (if you want to do as you did in your example) :

for (const item in countItems) {
    console.log(countItems[item])
    if (countItems[item] == "70") firstCounter++;
    
}
Pwoo
  • 19
  • 5
0

Object.values() and reduce() are both the right ideas. Taken together...

var countItems = {
    "aa":"70",
    "bb":"70",
    "cc":"80",
    "dd":"90",
    "ee":"90",
    "ff":"90"
};

let counts = Object.values(countItems).reduce((acc, value) => {
  if (!acc[value]) acc[value] = 0;
  acc[value]++;
  return acc;
}, {});

let [theFirstValue, theSecondValue, theThirdValue] = Object.values(counts)

console.log(theFirstValue, theSecondValue, theThirdValue);
danh
  • 59,538
  • 10
  • 90
  • 129
0
const countItems = [
  { data: 'aa', status: '70' },
  { data: 'bb', status: '70' },
  { data: 'cc', status: '80' },
  { data: 'dd', status: '90' },
  { data: 'ee', status: '90' },
  { data: 'ff', status: '90' },
]; 

var countValues = Object.values(countItems);
let obj ={}
for(let val of countValues){
  if(!obj[val.status]){
    obj[val.status] = 1
  }else{
    obj[val.status] += 1
  }
}
 console.log(obj)
  • You answered the wrong problem. I know how to solve this one, I didn't know how to solve the original one. I only gave this as an example. This doesn't solve the problem I asked. – sdsdc1 May 24 '22 at 23:59