JavaScript object - different type depending on context

I'm going to start by saying that I don’t think it is possible, but hoping that someone knows some special way in JavaScript for this. I have a JavaScript object that is defined as follows:

var t = { "fields": { "a": ["99", "98"] } }; 

I am looking for a way so that I can define the same array element twice with different values ​​that could be used in two different contexts: something like this:

 var t = { "fields": { "a": ["99", "98"], "a": "99|98" } }; 

If I requested it using the t.fields["a"] syntax, it would return "99 | 98", but if I requested it using t.fields["a"][0] , it would return " 99. " I understand that if I request an array without specifying an index, then it returns "99.98" as a string, but I need it to return records separated by the character "|", instead of ",".

+5
source share
3 answers

To achieve what you want, you have some options. Choose which option is best for you, depending on the coding style, environment, etc. Here you are.

Option 1

You can easily replace the .toString() Array.prototype to return values ​​separated by a vertical line ( | ) instead of a regular comma. You can do it as follows:

 Array.prototype.toString = function() { return this.join("|"); }; 

Now you can easily do what you want:

 var t = { "fields": { "a": ["99", "98"] } }; console.log("My array is: " + t.fields); // My array is: 99|98 console.log("Its first element is: " + t.fields[0]); // Its first element is: 99 

By the way, changing the default methods / properties of objects is not always the best choice, because some libraries or their own methods can use them and cause errors because they have been changed.

Option 2

To achieve what you want, if you only need to make several arrays, you can change their prototype instead of the standard Array.prototype , for example:

 // Let assume you have arrays a, b and c var a = [1, 2], b = [3, 4], c = [5, 6]; a.toString = b.toString = c.toString = function() { return this.join("|"); }; 

This is the best way if you work with only a few arrays, otherwise it would be difficult to change each prototype of each array you use and it is better to do this using Array.prototype (as in my first example).

Option 3

Create your own function and use it. This is recommended because it does not modify any existing prototype method, which may lead to some errors depending on how you use your arrays.

 // Create your own function function myJoin(array) { return array.join("|"); } var a = [1, 2, 3]; myJoin(a); // "1|2|3" // Or add it to the prototype Array.prototype.myJoin = function() { return this.join("|"); } var b = [11, 22, 33]; b.myJoin(); // "11|22|33" 
+2
source

You can also use the getter function, for example:

 var t = { "fields": { "_a": ["99", "98"], "a": function (index) { if (typeof index != 'undefined') return this._a[index]; else return this._a.join('|'); } } }; console.log(t.fields['a']()); // 99|98 console.log(t.fields['a'](1));// 98 

You can also get a global retrieval function for all fields:

 var t = { "fields": { "_a": ["99", "98"], get: function (field, index) { if (typeof index != 'undefined') return this['_' + field][index]; else return this['_' + field].join('|'); } } }; console.log(t.fields.get('a')); // 99|98 console.log(t.fields.get('a', 1)); // 98 
+3
source

Just to close this, thanks @ marco-bonelli and everyone else I decided with:

 var t = { "fields": { "a": ["99", "98"], "b": ["77", "76"] }, "token": "|" }; Object.keys(t).forEach(function(key) { t.fields[key].toString = function() { return this.join(t.token); }; }); 

The "t" object is dynamically generated from user data (with careful screening), so there can be several elements in the "field" object.

0
source

Source: https://habr.com/ru/post/1206003/


All Articles