Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the usage of eval() justified here?

The JavaScript object data has an attribute 'amplitudes' which is a string concatenated bunch of bitmask arrays coming from the server.

var data = {
  "amplitudes":           
  "[1,1,1,4,1,1],[1,1,1,1,1,1],[1,1,4,1,9,1],[1,1,9,1,16,1],[1,32,1,1,1,9],[4,4,4,1,1,1]"
}

.

This needs be broken down into six independant arrays. I am using a combination of split() and eval() to accomplish it in the following way:

var amplitudes = [];
amplitudes = data.amplitudes.split("],[");
for(var i=0;i<amplitudes.length;i+=1){
    (i+1)%2>0 ? amplitudes[i] = amplitudes[i] + "]" : amplitudes[i] = "[" + amplitudes[i];
    amplitudes[i] = eval(amplitudes[i]);
}

Questions


1) Is there a more elegant and efficient way to do this?? I am not too happy with the usage of eval(), but had a feeling split is more efficient than a regex? I haven't run a benchmark yet.

2) I am also open to manipulating the format in which the field 'amplitudes' is stored in database so that my overall design gets simpler.

Suggestions welcome

like image 219
paddle42380 Avatar asked Jan 21 '23 00:01

paddle42380


2 Answers

As you probably process your data with a server-side language simply make it generate a JavaScript array. If you have an array in your server-side code, use a JSON encoder to build the JavaScript object/array.

var data = {
  "amplitudes": [[1,1,1,4,1,1], [1,1,1,1,1,1], [1,1,4,1,9,1], [1,1,9,1,16,1], [1,32,1,1,1,9], [4,4,4,1,1,1]]
}

If you cannot change what you get from the server, you can do it using eval but in a much simpler way:

var str = "[1,1,1,4,1,1],[1,1,1,1,1,1],[1,1,4,1,9,1],[1,1,9,1,16,1],[1,32,1,1,1,9],[4,4,4,1,1,1]";
var arr = eval('[' + str + ']');
like image 136
ThiefMaster Avatar answered Jan 31 '23 12:01

ThiefMaster


If you can change the server, just have the "amplitudes" property be an array of arrays, and don't write it out with quotes:

var data = {
  amplitudes: [ [1, 1, 1, 4, 1, 1 ], [ 1, 1, 1, 1, 1, 1 ], ... ]
};

Now your client need do no decoding at all.

like image 33
Pointy Avatar answered Jan 31 '23 13:01

Pointy