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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:20:40+00:00 2026-06-14T02:20:40+00:00

I need to store data to represent this: Water + Fire = Steam Water

  • 0

I need to store data to represent this:

Water + Fire = Steam
Water + Earth = Mud
Mud + Fire = Rock

The goal is the following: I have draggable HTML divs, and when <div id="Fire"> and <div id="Mud"> overlap, I add <div id="Rock"> to the screen. Ever played Alchemy on iPhone or Android? Same stuff

Right now, the way I’m doing this is a JS object :

var stuff = {
    'Steam' : { needs: [ 'Water', 'Fire'] },
    'Mud'   : { needs: [ 'Water', 'Earth'] },
    'Rock'  : { needs: [ 'Mud',   'Fire'] },
    // etc...
};

and every time a div overlaps with another one, I traverse the object keys and check the ‘needs’ array.

I can deal with that structure but I was wondering if I could do any better?

Edit: I should add that I also need to store a few other things, like a short description or an icon name. So typicall I have Steam: { needs: [ array ], desc: "short desc", icon:"steam.png"},

Final edit: thank you everyone for contributing, I found really valuable input in all your comments

  • 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-14T02:20:41+00:00Added an answer on June 14, 2026 at 2:20 am

    If you don’t mind including another external library and are comfortable with LINQ, you can use linq.js for this.

    var stuff = {
      'Steam' : { needs: ['Water', 'Fire'] },
      'Mud'   : { needs: ['Water', 'Earth'] },
      'Rock'  : { needs: ['Mud',   'Fire'] }
        // etc...
    };
    
    function Alchemy(stuff) {
      var recipes = 
        Enumerable.From(stuff).ToLookup(
          "$.Value.needs",
          "$.Key", 
          "Enumerable.From($).OrderBy().ToString('+')"
        );
    
      this.attempt = function(elem1, elem2) {
        return recipes.Get([elem1, elem2]).ToString();
      };
    };
    
    var alchemy = new Alchemy(stuff);
    console.log(alchemy.attempt('Fire', 'Mud'));   // "Rock"
    console.log(alchemy.attempt('Fire', 'Earth')); // ""
    console.log(alchemy.attempt('Fire', 'Water')); // "Steam"
    

    Notes

    • Enumerable.From(stuff) splits your stuff object into its Key and Value parts.
      For instance, Key would refer to "Rock" and Value to { needs: ['Mud', 'Fire'] }.
    • ToLookup() creates a look-up dictionary from that. It takes 3 parameters:
      1. what to look for (in this case, the elements in "$.Value.needs")
      2. what to return if a match was found (in this case, the resulting element’s name, i.e. the Key)
      3. the transformation function that creates the dictionary key (in this case an array of ingredients is transformed into a sorted string: ['Mud', Fire'] becomes "Fire+Mud").
    • the Get() function finds a match for its argument, using the same transformation function.

    Note that a string argument like "$.Value.needs" is a shorthand for
    function ($) { return $.Value.needs; }.

    linq.js also provides many more useful functions that can transform complex tasks into one-liners.


    Edit: Returning all the additional info from the lookup would be as simple as:

    function Alchemy(stuff) {
      var recipes = 
        Enumerable.From(stuff).ToLookup(
          "$.Value.needs",
          null, // return the object unchanged 
          "Enumerable.From($).OrderBy().ToString('+')"
        );
    
      this.attempt = function(elem1, elem2) {
        return recipes.Get([elem1, elem2]).FirstOrDefault();
      };
    };
    
    console.log(alchemy.attempt('Fire', 'Mud')); 
    /* result
    {
      Key: "Rock",
      Value: {
        needs: ["Mud", "Fire"],
        whatever: "else you had defined in {stuff}"
      }
    }
    */
    

    The purpose of the Lookup object is to increase speed. You could also traverse the entire object graph every time:

    function alchemy(elem1, elem2) {
      return 
        Enumerable
        .From(stuff)
        .Where(function ($) {
          var recipe = Enumerable.From($.Value.needs);
          return recipe.Intersect([elem1, elem2]).Count() == 2;
        })
        .Select("{element: $.Key, properties: $.Value}")
        .FirstOrDefault();
    );
    
    console.log(alchemy('Fire', 'Water'));
    // {element: "Steam", properties: {needs: ["Water", "Fire"]}}
    

    Note that .Select() is optional. You could remove it, in which case the result would be the same as in the previous example.

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

Sidebar

Related Questions

I have 6000 data of district, subdistrict. I need to represent this on dependent
I need to store data, the problem is that I only know the name
I am creating a windows phone application, which need to store some data in
I'm writing a program in C# that will need to store a few Data
I need to store a lot of data coming in from a server into
I want to store some data during my site viewing. Sometime i need to
I´ve a Javascript object with all the data that I need to store on
Ok, I need to store/retrieve a bit from a data table of 3.268.760 bits
I need a way to store application-level data (i.e. cross user sessions) in ASP.NET.
I am in need of a lightweight way to store dictionaries of data into

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.