silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.18k stars 239 forks source link

Access object properties using brackets #264

Open coco-super opened 2 years ago

coco-super commented 2 years ago

Use the latest version and write the following code: Parser.evaluate('in1["key"]', { in1: { key: 'value' } });

I expect the above code to output "value", but got undefined

Can you spare some time to have a look? Thank you. @silentmatt

coco-super commented 2 years ago

I try to find answers in other issues. Find similar problems, but this is not what I want. #114

coco-super commented 2 years ago

https://github.com/silentmatt/expr-eval/blob/6e889e0e75c50ac37d70c35388602025650e0c50/src/functions.js#L253 The above code is modified as:

export function arrayIndex(array, index) {
  if (Array.isArray(array)) {
    return array[index | 0];
  }
  return array[index];
}

Then the world will be peaceful.