3

I have an object like this, that needs to be sent to an API -

var data = { 
    field : "category"
    value : "order"
}

Now, this API also needs some conditions needed to be sent as nested objects. Something like this -

var data ={ 
    field : "category"
    value : "order"
    AND : {
      field : "circuit"
      value : "ninth"
      OR : {
         field : "second"
         value : "abc",
         //more conditions here possible //
      }
}

So, basically, a condition is nested inside a condition. EDIT:: Note - Each condition added into the conditions array has to be nested into the previous condition

Now, I have an array of conditions -

conditionsArray = [
{filter: "AND", field: "circuit", value: "ninth"},
{filter: "OR", field: "second", value: "abc"}
]

How do I add conditions to data object from this conditionsArray in the most optimal way?

var data = { 
    field : "category"
    value : "order"
}

Thank you for your time.

EDIT: sorry, I forgot to add what I have tried so far -

I have been trying to just form an object but this is by no means a good solution.

const check = () => {
    console.log(conditionsArray);
    const data: any = { ...fieldValue };
    conditionsArray.forEach((condition: any) => {
      const { filter, name, value } = condition;
      const { AND, OR, NOT } = data;
      if (AND || OR || NOT) {
        if (AND) {
          data["AND"][filter] = { name, value };
        }
        if (OR) {
          data["OR"][filter] = { name, value };
        }
        if (NOT) {
          data["NOT"][filter] = { name, value };
        }
      } else {
        data[filter] = { name, value };
      }
    });
    console.log(data,"log data");
  };
Atin Singh
  • 3,624
  • 2
  • 18
  • 25
  • 2
    In your example, why does the OR condition get nested into the AND one? Should all conditions be nested into the previous one? – Aaron Oct 09 '20 at 08:54
  • Maybe a mix of these: [How can I add a key/value pair to a JavaScript object?](https://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object) and [How to map key/value pairs of a “map” in JavaScript?](https://stackoverflow.com/questions/54651873/how-to-map-key-value-pairs-of-a-map-in-javascript) – emerson.marini Oct 09 '20 at 08:54
  • @04FS yes my bad, I absolutely forgot to add my approach so far. – Atin Singh Oct 09 '20 at 08:55
  • @Aaron Yes each condition added into the conditons arrary has to be nested into previous condition. I'll add this part in question. – Atin Singh Oct 09 '20 at 08:56
  • @NinaScholz Sorry, I don't get what you mean. – Atin Singh Oct 09 '20 at 09:00
  • 1
    @NinaScholz I have an object called data `var data = { field : "category" value : "order" }` and have an array which has conditions `conditionsArray =[ {filter: "AND", field: "circuit", value: "ninth"}, {filter: "OR", field: "second", value: "abc"} ]` and I need to use this conditions array to nest these conditions into data object (by the order that they are in array ). Like this - `var data ={ field : "category" value : "order" AND : { field : "circuit" value : "ninth" OR : { field : "second" value : "abc", } }` – Atin Singh Oct 09 '20 at 09:04

3 Answers3

2

You could reduce the array and return the nested object for the next iteration.

const 
    data = { field: "category", value: "order" },
    conditionsArray = [{ filter: "AND", field: "circuit", value: "ninth" }, { filter: "OR", field: "second", value: "abc" }];

conditionsArray.reduce((o, { filter, ...rest }) => o[filter] = rest, data);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

If your conditionsArray items should be added in the order of appearance to data, you may go recursive way like that:

const conditionsArray=[{filter:"AND",field:"circuit",value:"ninth"},{filter:"OR",field:"second",value:"abc"}], 
      data={field:"category",value:"order"},
      
      makeATree = (o, list, branch) => {
        if(!list.length) return o
        const [condition, ...restOfConditions] = list,
              {filter, ...rest} = condition
        return makeATree(
          {
            ...o, 
            ...(
              !branch ? 
              {[filter]: rest} : 
              {[branch]: {...o[branch], [filter]: rest}}
            )
          }, 
          restOfConditions,
          filter
        )        
      }
      
console.log(makeATree(data, conditionsArray))
.as-console-wrapper{min-height:100%;}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
1

Using recursion :

function addConditions(data, conditions) {
    if (conditions.length == 0) { return data; }
    var topMostCondition = conditions[0];
    var objectToAdd = { field : topMostCondition.field, value : topMostCondition.value };
    data[topMostCondition.filter] = addConditions(objectToAdd, conditions.splice(1));
    return data;
}
Aaron
  • 24,009
  • 2
  • 33
  • 57