I'm building a script that tests if a variable is present in an object. To do that I use a regEx to test if that specific name is present in the object.
The problem I'm facing is that the JSON response is sometimes translated in German or Spanish.
So in below case I want to test spec.title for the name Length (mm). This title also can be Länge (mm) and Longitud (mm).
In my project there's an already build in translation function called getAjaxTranslation('string')
So this getAjaxTranslation('length') will return the translated string for length.
So my question is how can I use getAjaxTranslation('length') in below code to test?
So what I tried is:
reLength = '/'+ getAjaxTranslation('length') + '/i' //just to try
reLength = "'+ getAjaxTranslation('length') + '";
reLength = getAjaxTranslation('length')
Above tries give errors like reLength.test is not a function etc. So probably I'm working with wrong string/variables or doing something really wrong. I still have problems with those regExs...
Any help greatly appreciated!
Full code:
var data = {
"product": {
"specs": {
"231638": {
"id": 231638,
"title": "Length (mm)",
"value": "1200"
},
"231641": {
"id": 231641,
"title": "Width (mm)",
"value": "800"
},
"231644": {
"id": 231644,
"title": "Height (mm)",
"value": "144"
} //etc etc
}
}
};
var length = 0, width = 0, height = 0,
reLength = /length/i,
reWidth = /width/i,
reHeight = /height/i;
$.each(data.product.specs, function (specId, spec) {
if (reLength.test(spec.title))
length = spec.value;
else if (reWidth.test(spec.title))
width = spec.value;
else if (reHeight.test(spec.title))
height = spec.value;
});