Skip to content
Advertisement

Traversing recursively through an array and modifying values of object properties in JavaScript

The post may seem lengthy but it’s quite easy to follow, if not, I will add more details

I have criteria array which that looks like :

JavaScript

Characteristics of criteria:

  1. It could have nested arrays.
  2. First item array (or nested array) is always going to be either “and” or “or”
  3. Second item onwards in array, item could be either an object with this specific structure

{ "Collection": "persons", "Property": "phone", "operator": "eq", "operatorValue": "$p:phone" }

or it could be an array like:

["or", { "Collection": "persons", "Property": "city", "operator": "eq", "operatorValue": "$p:city" }]

  1. Object is never going to be nested object

There is also a parameters object:

JavaScript

The aim is to recursively traverse through all the operatorValue properties in the criteria array and if you encounter value such as $p:phone, it is to be replaced by whatever parameters["phone"] evaluate to.

EXPECTED OUTPUT:

JavaScript

I was able to recursively traverse through the array. The only problem is that I can’t figure out how to modify the original criteria variable.

REPL

Please see the line 43 in repl. item[1]=parameters[item[1].split('$p:')[1]] I understand why it won’t modify the criteria, its because item over here is a different variable in different scope altogether.

Failed Attempt :

JavaScript

How do I go about solving this problem?

Advertisement

Answer

You could simplify your function. You don’t need to loop through the entries of the object. You also don’t need to split the operationValue. That mapping key of parameters is present in the Property key.

  • Loop through each item in the array and check if the item is an Array.
  • If yes, recursively call traverse on the item.
  • If it is an object, update its operatorValue property with parameters[val.Property]

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement