Jump to content

Object property names as strings?


Recommended Posts

Hey all, Is there anything wrong with using a string for a object property name/key?


for example:


var swaps = {

'this' : 'that'



My goal is to create an object containing code that needs to be swapped based on certain conditions and I'd like to avoid having to loop through 2 arrays or objects to find the swap with a matching index.


Thanks in advance!

Link to comment
Share on other sites

Object property names are technically strings to begin with.


There's no difference between {"x": 1} and {x: 1}


In JSON, the property name actually requires double quotes.


If you want to be able to pass a variable string as an object property, the square brackets will do it.

var position = {  x : 1,  y : 2}var axis = "x";alert( position[axis] ); // Displays "1"
  • Like 1
Link to comment
Share on other sites

Well just incase anyone is interested, I thought about it more and changed my approach. To reiterate, the goal was to change specific lines of code with something else based on which subdomain your on.


so for example:


on www.example.com show "WWW Heading"

on sub1.example.com show "Sub 1 Heading"

on sub2.example.com show "Sub 2 Heading"


the 3 ways I could think of for doing this were:



Option 1: create an array for the original string and one for each sub domain:

var original = [    'WWW Heading',    'img/www/img.png'];var sub1 = [    'Sub 1 Heading',    'img/sub1/img.png'];var sub2 = [    'Sub 2 Heading',    'img/sub2/img.png']; 

then looping through them and replacing original with sub1 or sub2. The issue with this approach is that as the number of swaps grows, it could be hard to maintain. The possibility of error grows as the original and the substitutes are separated from each other and tracking which line goes with which could be difficult in a large list.



option 2: create an object for each subdomain containing the original and its substitute:

sub1 = {    'WWW Heading' : 'Sub 1 Heading',    'img/www/img.png' : 'img/sub1/img.png'}sub2 = {    'WWW Heading' : 'Sub 2 Heading',    'img/www/img.png' : 'img/sub2/img.png'}

Then use a for in loop to loop through the sub object and replace property with sub1[property]. The issue with this approach is that I'm repeating the original strings for each sub domain.



Option 3 (which I think I'm going to go with): create an array of objects for each original string and it's substitutes:

swaps = [    {        original: 'WWW Heading',        substitutes: {            sub1: 'Sub 1 Heading',            sub2: 'Sub 2 Heading'        }    },    {        original: 'img/www/img.png',        substitutes: {            sub1: 'img/sub1/img.png',            sub2: 'img/sub2/img.png'        }    }]

Then I loop through the swaps and replace the swaps.original with swaps.substitutes[domain]. This probably isn't the best preforming, but it is much easier to read and maintain.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...