0

I have a problem...

I have a string that inputted by the user and looks something like this

let conditions = "id === 101 || name === 'Albert' && age !== 43"

I also have an array of data that was also created by the user at some point previously The data was stored in the form of an array that looks like so, and the user now has requested all data entries that match the conditions he has given.

let data = [
    {id:101, name:"Albert", age:63},
    {id:102, name:"Einstein", age:53}
]

You can probably see where I'm going with this. The array to be returned should look like this.

let returnedArray = [
    {id:101, name:"Albert", age:63}
]

Now, my first thought was just to use eval. Creating the variables with eval and for loop, checking with eval and so on.

But that would be very slow and seems redundant. Also, we can no longer declare variables with eval, and creating so many global variables would be quite harmful to my project.

Neither the conditions or the data array are static. The conditions are user inputed and the data arrays are not only ever changing, they are also user created.

Any suggestions?

Dark Programmer
  • 482
  • 3
  • 6
  • 18
  • 1
    Create a parser. – max Jan 14 '22 at 16:05
  • 1
    Be careful about using `001`. Leading zeroes mean the number is interpreted as octal (and might cause problems if you try something like `009` – byxor Jan 14 '22 at 16:06
  • 2
    I hope your string actually looks like: `"id === 001 || name === \"Albert\" && age !== 43"` or `"id === 001 || name === 'Albert' && age !== 43"`; as written, it's a syntax error. – Patrick M Jan 14 '22 at 16:07
  • @byxor, thanks! I've changed the values to be on the safer side in the example code – Dark Programmer Jan 14 '22 at 16:10
  • @PatrickM Thanks! I've corrected my oversight in the example code. The actual code in my project doesn't have the same mistake – Dark Programmer Jan 14 '22 at 16:11

1 Answers1

3

Using eval would be the simplest way, there is no need to create a ton of global variables. We can use filter, and destructure each object into its properties which happen to match the condition's logic.

let conditions = "id === 001 || name === 'Albert' && age !== 43"

let data = [
    {id:001, name:"Albert", age:63},
    {id:002, name:"Einstein", age:53}
]

let output = data.filter(({id, name, age}) => eval(conditions));

console.log(output);

Another possibility is to change the structure of conditions so it's code instead of strings, removing the dangers of using eval:

let conditions = ({id, name, age}) => {
  return id === 001 || name === 'Albert' && age !== 43;
};

let data = [
    {id:001, name:"Albert", age:63},
    {id:002, name:"Einstein", age:53}
]

let output = data.filter(conditions);

console.log(output);
James
  • 20,957
  • 5
  • 26
  • 41
  • Yup, the first one works perfectly well, except for one problem. You see, the user enters the condition in that format as a part of a wider command. Think SQL SELECT where the condition is the WHERE statement. I'll have no way of knowing which or how many values they want to check for. It could be only id or id and name or only age. And these three parameters are not fixed either. They could ask for birthday in a whole another set of data too! – Dark Programmer Jan 14 '22 at 16:37
  • I've edited the question to address the constraints.. Apologies for not mentioning it before – Dark Programmer Jan 14 '22 at 16:44