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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:46:59+00:00 2026-06-03T21:46:59+00:00

The Python idiom in the subject line set(‘pdf ppt tif tiff jpg jpeg’.split()) illustrates

  • 0

The Python idiom in the subject line

set('pdf ppt tif tiff jpg jpeg'.split())

illustrates a fairly compact way to create a set of strings.1 I’m looking for a similarly compact idiom to create a set in JavaScript. (NB: I am not interested in merely creating a list of strings.)

I understand that the standard approach to sets in JS is to use objects (keys are set elements, values immaterial), but I have not hit upon a comparably succinct way to create such a set in JS.

FWIW, the idiom I’m looking for is intended for a bookmarklet (hence the emphasis on succinctness).

Thanks!

1 For those unfamiliar with Python, the inner expression just evaluates to the list of strings obtained by splitting the invoking string object at the whitespace; this list is then the argument to the set constructor.

Edit: emphasized that I’m looking for a set (i.e. object), not a list of strings.

  • 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-03T21:47:02+00:00Added an answer on June 3, 2026 at 9:47 pm

    JavaScript doesn’t have a set type but you need a helper function to easily create it from a string (or from an Array).

    Here’s such a helper function which will allow you to call .set() on a string and get an object mapping the items from the string to some value (true if none is specified):

    String.prototype.set = function(val) {
        var items = this.split(' '),
            len = items.length,
            obj = {},
            i;
        for(i = 0; i < len; i++) {
            obj[items[i]] = val === undefined ? true : val;
        }
        return obj;
    };
    

    Demo:

    > 'pdf ppt tif tiff jpg jpeg'.set()
    { pdf: true,
      ppt: true,
      tif: true,
      tiff: true,
      jpg: true,
      jpeg: true }
    

    Since you prefer something succinct, this might be a solution for you if you only need it for a single string:

    var set = (function(s){ var obj = {}; for(var i = 0, elems = s.split(' '), l = elems.length; i < l; i++) obj[elems[i]] = 1; return obj; })('pdf ppt tif tiff jpg jpeg');
    

    In that case I would consider simply using an object literal though… that’s most likely the shortest solution unless you have a ton of elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, I know this subject has been covered before: Python idiom to chain (flatten)
I usually use the following idiom when working with a Python dictionary: try: val
I'm just wondering, is there a Python idiom to check if a string is
What's the closest thing in Java (perhaps an idiom) to threading.Event in Python?
I just came across this idiom in some open-source Python, and I choked on
When reading lines from a text file using python, the end-line character often needs
Possible Duplicate: What is the clojure equivalent of the Python idiom “if name ==
What's the best Python idiom for this C construct? while ((x = next()) !=
An idiom commonly used in OO languages like Python and Ruby is instantiating an
Quick question about Python(ic) inheritence: what is the difference between the idiom posted here

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.