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 6575423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:23:05+00:00 2026-05-25T15:23:05+00:00

I have two arrays. For example: var bool = [true, false, true]; var vals

  • 0

I have two arrays. For example:

var bool = [true, false, true];
var vals = ['hi','med','lo'];

Using jQuery, I’d like to loop through them so that I can test each bool, and set a var equal to the highest available value from the vals array after the first true from the bool array:

var value = ( bool[0] ) ? vals[0] || vals[1] || vals[2] :
                        ( bool[1] ) ? vals[1] || vals[2] : vals[2] ;

I’d like to make this work in a way that the arrays could have more values (but the size of the one would always match each.) Is it possible to pull this off with an .each function?

  • 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-05-25T15:23:07+00:00Added an answer on May 25, 2026 at 3:23 pm

    EDIT: It appears that you need the first truthy value from vals and bool. Here’s a solution that doesn’t require any functions.

    var value, i = 0;
    
    while( !bool[i] && ++i < bool.length );
    
    while( !(value = vals[i]) && ++i < vals.length );
    

    I misunderstood the question to think that the arrays need to be truthy at the same indices. Now I see that the first true bool index is just the starting point for vals. Fixed.


    To explain, we have two while loops. In both of them, the typical {...} block statement has been replaced by an ; empty statement. This is because all the work is done by the expressions between the (...).

    The first loop simply increments i until a truthy value is found in bool. That sets i to the starting point for the second loop.

    The second loop does the same thing as the first, except that each time the expression runs, it sets the current val[i] to the value variable. It does this as long as value is assigned a “falsey” value.

    This is effectively the same as your val[0] || val[1] || val[2] part, except it will always begin wherever the bool loop left i.

    So once value gets a “truthy” value, or i exceeds the last index in the vals array, the loop will quit. At this point, value will hold either the “truthy” value that was found, or undefined if none was found.


    As a function:

    DEMO: http://jsfiddle.net/W9uBb/

    function filterValsFromBools(bool, vals) {
        var value, i = 0;
    
        while (!bool[i] && ++i < bool.length);
    
        while (!(value = vals[i]) && ++i < vals.length);
    
        return value;
    }
    
    var a = filterValsFromBools(
        [true, false, true], 
        ['hi', 'med', 'lo']
    );
    
    var b = filterValsFromBools(
        [false, true, true], 
        ['hi', 'med', 'lo']
    );
    
    var c = filterValsFromBools(
        [false, true, true], 
        ['hi', 0, 'lo']
    );
    
    console.log( a, b, c );  // "hi" "med" "lo"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of values like X,Y,Z and 1,2 There is a table
Considering I have two arrays for example: String[] array1 = new String[10]; int[] array2=
I have two arrays of objects: var a = [ {'id': 20}, {'id': 15},
I need to do operations like compare that two number arrays have the same
I have some code that appears to merge the data from two arrays using
Hi I have a text file containing two arrays and one value(all integers) like
If I have an array like this: var arr = ['one','two','three']; I can access
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays. One contains id=>count and the other contains id=>name . I'm

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.