Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:41:57+00:00 2026-06-01T08:41:57+00:00

I have an array in jQuery, and I need to count the number of

  • 0

I have an array in jQuery, and I need to count the number of “true” strings in that array, and then make the “numOfTrue” variable equal the number of true strings. So in the below array, there are 2 “true” strings, so numOfTrue would be equal to 2.

var numOfTrue;
var Answers = [ "true", "false", "false", "true", "false" ];

I’m not sure how to do a loop through the array in jQuery to count the strings. Or is a loop even necessary?

The number of true strings could change from anywhere between 1 to 5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:41:58+00:00Added an answer on June 1, 2026 at 8:41 am

    Using a basic, old-fashioned loop:

    var numOfTrue = 0;
    for(var i=0;i<Answers.length;i++){
        if(Answers[i] === "true")
           numOfTrue++;
    }
    

    or, a reduce

    var numOfTrue = Answers.reduce((acc,curr) => {
        if(curr === "true")
           acc++;
        return acc;
    },0);
    

    or a filter

    var numOfTrue = Answers.filter(x => x === "true").length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mixed array that I need to sort by alphabet and then
I'm progamming in javascript/jQuery. Say, I have an array with includes a few number
i have array in php, and i need work with this array in jQuery.
I have an array (for checkboxes) that I need to pass alongside the regular
I have a jquery auto complete script that takes a JSON array and displays
Using javascript/jQuery: I have an array with images and I need to display them
I need a jquery autocomplete that only does one call to the server then
I am trying to work out a complicated jQuery selection. I have an array,
I am using jQuery 1.7.1 (but that's probably irrelevant). I have several JavaScript arrays
I have array of strings, String[] data and it's 10 elements has value P

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.