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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:25:24+00:00 2026-06-17T15:25:24+00:00

I need the following two codes 1) A code to select all variables that

  • 0

I need the following two codes
1) A code to select all variables that begin with “example”
2) A code to select all variables that have “true” as value for “available”

example1= {price:1000, size: 1000, available:true}
example2= {price:2000, size: 2000, available:false}
example3= {price:3000, size: 3000, available:true}
example4= {price:4000, size: 4000, available=true}

This is what I want to achieve with code one. As there are a lot of variables I need a quick way of doing it:

var allexampleprices=[example1.price, example2.price, example3.price, example4.price]

With the second code I want to get an array with all the names of the variables that contain the value “false”

Any help appreciated!

  • 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-17T15:25:25+00:00Added an answer on June 17, 2026 at 3:25 pm

    All of these are the exact same thing, assuming you’re not in a function:

    var myVar       = 7;
    window.myVar    = 7;
    window["myVar"] = 7;
    

    Therefore, you can access any global variable (a variable defined outside a function) by using the window[ insertString ] method. If you wanted to search through every property on the window object to find one called example, you’d do:

    for( var k in window ){
      if(/example/.test(k)){
        var myExample = window[k];
        // Do stuff
      }
    }
    

    I would HIGHLY recommend against this method, though, for many reasons. To start, it’s a horribly bad practice to put anything in the global scope. Variables will start colliding all over the place on big projects. Also, the window object has soooooo many properties that searching through all of them is a horrible performance drain.

    Having said all of that, I’ve devised an example of what you should do, including the helper functions to do it:

    var objects =
    {
      example1:
      {
        price: 1000,
        size: 1000,
        available: true
      },
    
      example2:
      {
        price: 2000,
        size: 2000,
        available: false
      },
    
      example3:
      {
        price: 3000,
        size: 3000,
        available: true
      },
    
      example4:
      {
        price: 4000,
        size: 4000,
        available: true
      }
    }
    
    function filter(obj, comparator){
      var list = [];
      for(var k in obj){
        if(obj.hasOwnProperty(k)){ // fix for IE
          if(comparator(obj[k], k, obj)) list.push(obj[k]);
        }
      }
      return list;
    }
    
    function isExample(obj, key){
      if(/^example/.test( key.toLowerCase() )) return true;
    }
    
    function isAvailable(obj){
      if(obj.available) return true;
    }
    
    /**
     * And here's how you use it
     */
    
    var examples  = filter(objects, isExample);
    var available = filter(objects, isAvailable);
    
    var availableExample = filter(examples, isAvailable);
    

    The filter function returns an array of all of the matching objects.

    — EDIT —

    You want it to say the names of the objects in the console. I’m assuming what you mean is that the console currently shows [object, object, object, object]. There are two ways to do this:

    (1) Put the name in the object itself

    example1:
    {
      name: "example1",
      price: 1000,
      size: 1000,
      available: true
    }
    

    (2) Capture the name in the filter operation

    var names = [];
    var examples  = filter(objects, function(obj, name){
      if(/^example/.test( name.toLowerCase() )){
        names.push(name);
        return true;
      }
    });
    
    console.log(names);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1I have the following two tables (sample data) and need to be able to
I have the following three arrays and need to create a new two-dimensional array
i have the following query to list the employees of two table. i need
I have two tables (z and z_ao). z contains all the zip codes in
I have two table TABLE_EXAM and TABLE_QUESTION. I fetch record using following code but
I have the following code that I have been working on: var menuItems =
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need the following xml to be made in code: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:gravity=right
I need the following logic. If array contains value , return it else return
I have the following problem I have 3 table (all of the are used

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.