How to include this javascript string "myArray [0] .myPrice" in myPrice link?

If I have the line "myArray[0].myPrice" , how do I include this link in the myPrice link?

This is the code context:

 binding.value = convert(binding.data[binding.field], binding.converter, binding.data, elem); 

binding.field is what contains "myArray [0] .myPrice".

binding.data has a reference to a hierarchical object that has the Array property myArray, and the first element is an object with the myPrice property.

EDIT: based on the answers this worked:

 binding.value = convert(eval('(binding.data.' + binding.field + ')'), binding.converter, binding.data, elem); 

Is it good to use eval like that? I thought eval goes into new JavaScript?

+1
source share
5 answers

You can use eval , but here is the best solution:

 // The following code results in the same as that scary eval(...) var data = binding.data, chain = binding.field.split(/[\.\[\]]+/); for (var i = 0; data[chain[i]]; i++) { data = data[chain[i]]; } // Embrace JavaScript Awesomeness! 

A breakdown of what I'm doing here:

In JS, any property can be called object[propertyName] .

This includes arrays, i.e. a[3] same as a['3'] .

Therefore, we will split the string into one of the characters:. , [ , ] . + exists, because without it, if you have a[3].b[3] , ]. will give you an empty string.

We can get an empty string at the end, but this is not a problem, since "" is like false in JS.

You can go ahead and filter out all invalid variable names that in javascript is a name that does not match [a-zA-Z_$][0-9a-zA-Z_$]* . But I'm not quite sure why this can be done ...

+2
source

Well, this is not entirely beautiful, but it does not use eval :

 var str = 'myArray[0].myPrice'; var parts = str.split(/[\[\].]+/); doSomething(window[parts[0]][parts[1]][parts[2]]); 

where window[parts[0]][parts[1]][parts[2]] is the actual link. Not sure why you will have a string version and loaded into memory.

+1
source

Depending on the current context, this may work:

 eval("myArray[0].myPrice") 
0
source

to try

 var a = eval("myArray[0].myPrice".split('.')[1]); 

However, this probably will not work in the context in which you run it, since myPrice will reference window.myPrice , which I doubt will be defined as anything.

It is best to define myArray in an area that can be accessed from anywhere (e.g. window.AppNS.Data.myArray ), and then use the eval() function to access it.

 var a = eval("window.AppNS.Data." + "myArray[0].myPrice") alert(a) // the contents of myPrice are revealed 
0
source

Here is something similar to the logic we use in the pure.js template engine

 var obj = { myArr:[ {price:15} ], anObj:{ prop:'ABC' } }; function getRef(o, path){ var props = path.split(/\./), prop, isArr = /([^\[]+)(\[(\d+)\])*/, i = 0, ii = props.length; while(i < ii){ prop = props[i].match(isArr); o = o[prop[1]]; if(prop[2]){ o = o[+prop[3]]; } i++; } return o; } console.log(getRef(obj, 'myArr[0].price')); // --> 15 console.log(getRef(obj, 'anObj.prop')); // --> ABC 
0
source

All Articles