Home > Mobile >  How to count values in a javascript object?
How to count values in a javascript object?

Time:05-25

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?

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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  ;
    
}

CodePudding user response:

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);

CodePudding user response:

this is what i found to work...

Object.keys(countItems).map(key => {
    if(countItems[key] === "70") {
        return seventy  
    } else if(countItems[key] === "80") {
        return eighty  
    } else if(countItems[key] === "90") {
        return ninety  
    }
    return
});

What is Object.keys()?

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon an object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

I used that function to create an array out of the object, and then to map that array to match the values to either 70, 80, or 90 and then adding 1 to the appropriate variable.

CodePudding user response:

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)
  • Related