4

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");
  };
6
  • 2
    In your example, why does the OR condition get nested into the AND one? Should all conditions be nested into the previous one? Commented Oct 9, 2020 at 8:54
  • Maybe a mix of these: How can I add a key/value pair to a JavaScript object? and How to map key/value pairs of a “map” in JavaScript? Commented Oct 9, 2020 at 8:54
  • @04FS yes my bad, I absolutely forgot to add my approach so far. Commented Oct 9, 2020 at 8:55
  • @Aaron Yes each condition added into the conditons arrary has to be nested into previous condition. I'll add this part in question. Commented Oct 9, 2020 at 8:56
  • 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", } } Commented Oct 9, 2020 at 9:04

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

Comments

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;
}

1 Comment

More solutions are always welcome. And this works perfectly. Thanks.
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%;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.