-2

I have two variables with the name condition and Data. need to find condition fields key in Data variable. need to reassign the value in different variable.

let condition = {
                "velocity_global_sync": true, 
                "allowSync": false
            }

let Data = {
                "organization_id": 4002400004,
                "organization_name": "Velocity Global Integration Sandbox",
                "payload": {
                    "offer": {
                        "application_id": 34587141004,
                        "allowSync": true,
                        "custom_fields": {
                            "velocity_global_sync": {
                                "name": "Velocity Global Sync",
                                "type": "boolean",
                                "value": true
                            },
                        },
                        "id": 4555342004
                    }
                }
            }   

condition will not be static. Need to find the keys (velocity_global_sync, allowSync) from Data variable and return the keys value. response object should be like below

let response = {
            "velocity_global_sync": true, // velocity_global_sync value have same value from Data velocity_global_sync value so, its return true
            "allowSync": false // allowSync value have different value from Data allowSync value so, its return false.
        }
  • 1
    Can you show your current code which you are stuck with and some example outputs? In your question `velocity_global_sync` appears as a boolean and an object; it isn't clear what exactly you are asking for – Ash Oct 04 '22 at 12:56
  • I have edited the question. if i got response value with data variable's value that's enough – mani kandan Oct 04 '22 at 12:57
  • I'm still not clear. `velocity_global_sync`'s value and `velocity_global_sync.value` are not the same thing. Are you saying any truthy response is good enough to be considered `true` when looking for a match? – Ash Oct 04 '22 at 13:01
  • yes @Ash if both condition's velocity_global_sync and Data's velocity_global_sync value is true. we need to return true. otherwise need to return false. that's my requirement – mani kandan Oct 04 '22 at 13:03

2 Answers2

1

One approach which I have shown below, will first flatten the data response object to only contain the keys of nested objects and store the value as either true or false based on the comment that truthy values are good enough for comparison. Once that flatten object exists then we can compare the condition properties with the flattened data by index and test if the values are equal.

To flatten the data I borrowed the implementation from https://stackoverflow.com/a/55251598 with a slight modification to convert values to a boolean.

const condition = {
  "velocity_global_sync": true,
  "allowSync": false
}

const Data = {
  "organization_id": 4002400004,
  "organization_name": "Velocity Global Integration Sandbox",
  "payload": {
    "offer": {
      "application_id": 34587141004,
      "allowSync": true,
      "custom_fields": {
        "velocity_global_sync": {
          "name": "Velocity Global Sync",
          "type": "boolean",
          "value": true
        },
      },
      "id": 4555342004
    }
  }
}

// modified version of answer https://stackoverflow.com/a/55251598
// return flattened as boolean key/value
const flattenObject = (obj) => {
  const flattened = {}

  Object.keys(obj).forEach((key) => {
    const value = obj[key]

    if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
      Object.assign(flattened, flattenObject(value))
    }
    flattened[key] = !!value // this part is modified
  })

  return flattened
}

// flatten your data object
const dataToBooleanKeyValue = flattenObject(Data);
console.log('boolean key/value:', flattenObject(Data));

// follow similar approach to above but using `.reduce()`
// iterate over each condition property and find the corresponding property in data
// return object with property name and true if both value match, otherwise false
const compareKeysInData = (condition, obj) => {
  return Object.keys(condition).reduce((acc, key) => {
    if (obj[key] === condition[key]) {
      acc[key] = true;
    } else {
      acc[key] = false;
    }

    return acc;
  }, {});
}

const result = compareKeysInData(condition, dataToBooleanKeyValue);
console.log('result:', result);

Ash
  • 11,292
  • 4
  • 27
  • 34
0

You can do this:

<script>
let condition = {
                "velocity_global_sync": true, 
                "allowSync": false
            };

let Data = {
                "organization_id": 4002400004,
                "organization_name": "Velocity Global Integration Sandbox",
                "payload": {
                    "offer": {
                        "application_id": 34587141004,
                        "allowSync": true,
                        "custom_fields": {
                            "velocity_global_sync": {
                                "name": "Velocity Global Sync",
                                "type": "boolean",
                                "value": true
                            },
                        },
                        "id": 4555342004
                    }
                }
            } ;
            
let velocity_global_sync = (Data.payload.offer.custom_fields.velocity_global_sync.value == condition.velocity_global_sync);

let allowSync = (Data.payload.offer.allowSync == condition.allowSync);

let response = {
            "velocity_global_sync": velocity_global_sync,
            "allowSync": allowSync
        }


// Log to console
console.log(response);
</script>