1

Javascript newbie here. I currently have an associative array in the following format:

StringA: number,
StringB: number

Is it possible to map multiple strings to the same number? Something like this (some numbers may have a different number of Strings mapped to them):

StringA, StringB, StringC: number,
StringD, StringE: number,
StringF, StringG, StringH, StringI: number

Basically, I want holder to have the same value whether I write var holder = arr[StringA] or var holder = arr[StringC]. If it's not possible could someone point me in the right direction? Any help would be greatly appreciated!

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
Nishant
  • 131
  • 2
  • 12
  • you can use a var placeholder for the number, use a `swtich()` with overloaded `case`es, or hard-code them like `o.a=o.b=o.c=123;` – dandavis Jan 17 '17 at 21:16
  • do you also need to change the number? and it should be applied to all of the strings? – Aᴍɪʀ Jan 17 '17 at 21:17
  • @Aᴍɪʀ Yes, each number is updated and would apply to all strings that are mapped to it – Nishant Jan 17 '17 at 21:21
  • @Nishant So you can not simply use primitive numbers, the answer posted below should work. You need to wrap the number inside an object so it can be copied by reference. – Aᴍɪʀ Jan 17 '17 at 21:22
  • 1
    @Aᴍɪʀ: it _could_ work in two way: using an object as a number with a custom getter function, or using a getObject() function to re-assess the var names each time. bottom line: needs another function – dandavis Jan 17 '17 at 21:32
  • @dandavis If you have time could you write out an example of how that would would work? – Nishant Jan 17 '17 at 21:49
  • @Nishant: `var n={_:4},o={a:n,b:n}; n.valueOf = function(){return this._}; alert(o.a*o.b);n._=10;alert(o.a*o.b);` and the other way: `n=5; function o(){return {a:n, b:n}}; alert(o().a*o().b); n=12; alert(o().a*o().b);`, or cache `o()` just before the math... – dandavis Jan 17 '17 at 21:59

2 Answers2

0

You could use an object with a value for the object with multiple keys for one object.

Basically this creates a reference to the shared object. Then you could change the value inside of the object.

var temp = { value: 42 },
    object = {
        a: temp,
        b: temp,
        c: temp
    };

console.log(object.a.value); // 42
object.b.value = 7;
console.log(object.c.value); // 7
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • of course if anything is dynamic, this breaks down without functions. – dandavis Jan 17 '17 at 21:34
  • @dandavis, that's right, but maybe the data structure is questionable. – Nina Scholz Jan 17 '17 at 21:39
  • @NinaScholz why don't you have to write object.b.value.value = 7; to change the value? – Nishant Jan 17 '17 at 21:47
  • @Nishant, i changed the name of `value` to `temp`. the variable temp is only for keeping the reference to the object `{ value: 42 }`. after assigning to a property of `object`, you have no reference to the variable, only the content. – Nina Scholz Jan 17 '17 at 21:54
0

Basically Js don't have associative array they are objects. Read this: http://www.w3schools.com/js/js_arrays.asp

You need pointers to achive this, but JS not have pointers. But ther is a way to use pointers: use objects.

        var assoc = [];
        assoc["stringA"] = { value: 1};
        assoc["stringB"] = assoc["stringA"];
        assoc["stringC"] = assoc["stringA"];

        assoc["stringD"] = { value: 10};
        assoc["stringE"] = assoc["stringD"];


        console.log("A: "+assoc["stringA"].value);
        console.log("B: "+assoc["stringB"].value);
        console.log("C: "+assoc["stringC"].value);
        console.log("D: "+assoc["stringD"].value);
        console.log("E: "+assoc["stringE"].value);
        console.log("======== modify value ======");
        console.log("\nModify A to 2")
        assoc["stringA"].value = 2;
        console.log("B: "+assoc["stringB"].value);
        console.log("C: "+assoc["stringB"].value);

        console.log("\nModify E to 20")
        assoc["stringE"].value = 20;
        console.log("D: "+assoc["stringD"].value);
        console.log("E: "+assoc["stringE"].value);
robcaa
  • 181
  • 2
  • 2
  • 14
  • instead of a named prop, you might want to mention overloading `valueOf()` so it can be treated like a normal Number instance... – dandavis Jan 17 '17 at 21:33