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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:01:41+00:00 2026-05-28T15:01:41+00:00

Given a dictionary-like object in Javascript such as {a:1, b:-2, c:42} , is there

  • 0

Given a dictionary-like object in Javascript such as {a:1, b:-2, c:42}, is there a simple way to randomly choose a property?

In the above example, I would like to have a function that would return a, b or c randomly.

The solution I’ve come up with is like the following:

    var proplist = []
    forEach(property in foo) {
        if(propertyIsEnumerable(foo[property]) {
            proplist.push(property);
        }
    }
    var n = proplist.length;
    // randomly choose property (randInt(n) returns a random integer in [0,n))
    proplist[randInt(n)];

Is there a more idiomatic way to do this?

  • 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-28T15:01:42+00:00Added an answer on May 28, 2026 at 3:01 pm

    Use Object.keys (or even Object.getOwnPropertyNames) to get a list of all properties. Then, select a random property by multiplying Math.random() with the length of the list, floored.

    var propList = {}; //...
    var tmpList = Object.keys(propList);
    var randomPropertyName = tmpList[ Math.floor(Math.random()*tmpList.length) ];
    var propertyValue = propList[randomPropertyName];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a Python object of any kind, is there an easy way to get
Given a dictionary like so: my_map = {'a': 1, 'b': 2} How can one
Given a dictionary of <string, Drink> , how would you bind the dictionary.Values to
How can I set an object property given its name in a string? I
I want to associate a JavaScript object with an HTML element. Is there a
I'm looking at reducing the memory consumption of a table like collection object. Given
Given this powershell code: $drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]' $drivers.Add(nitrous,vx) $drivers.Add(directx,vd) $drivers.Add(openGL,vo) Is it possible
I have an NSArray of Object that has an interesting property that I would
Given a dictionary (just a list of strings). You receive a feed of an
Given a dictionary of words and an initial character. find the longest possible word

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.