2

Consider the following object:

{
  "params": {
    "time_to_diagnosis": [
      {
        "field": "date_of_diagnosis",
        "value": ""
      },
      {
        "field": "date_of_symptom_onset",
        "value": "2019-09-01"
      }
    ],
    "time_since_onset": [
      {
        "field": "date_of_symptom_onset",
        "value": "2019-09-01"
      }
    ]
  }
}

As you can tell this is a object , of objects with arrays that them selves contains objects.

As you can see some keys are empty.

The idea is that if there are no empty keys in the arrays containing objects, then return true, else return false.

Heres what I wrote:

const isParamsInAjaxParamsEmpty = (paramsForAjaxCall) => {
  for (const key in paramsForAjaxCall) {
    for (const nestedKey in paramsForAjaxCall[key]) {
      const params = paramsForAjaxCall[key];

      if (params[nestedKey] === "") {
        return true;
      }
    }
  }

  return false;
}

I Know I can do an Array.isArray on the nestedKey part, but Im not sure how to make this recursive, as there could be one or more arrays.

paramsForAjaxCall is the object above.

Thoughts?

1
  • 1
    isParamsInAjaxParamsEmpty suggests this is for posting to a server. If a form created this, I would do validation on the form instead. It's much simpler to report an error to the user than this way. Commented Sep 6, 2019 at 18:56

2 Answers 2

1

you can map the array then take the values from the Object and with every will get a boolean value so it's an array of booleans in the end because we mapping.

if this array contains a false so the result is false

const data = {
  "params": {
    "time_to_diagnosis": [{
        "field": "date_of_diagnosis",
        "value": "ddd"
      },
      {
        "field": "date_of_symptom_onset",
        "value": "2019-09-01"
      }
    ],
    "time_since_onset": [{
      "field": "date_of_symptom_onset",
      "value": "2019-09-01"
    }]
  }
}

const res = !Object.values(data).map(o => Object.values(o).map(value => value.every(({
  value
}) => value !== ""))).flat().includes(false)


console.log(res)

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

Comments

1

You could take a check for not object and return false, then check for the wanted property or iterate all properties.

function check(object) {
    if (!object || typeof object !== 'object') return false;
    if (object.value) return true;
    return Object.values(object).every(check);
}

var object = { params: { time_to_diagnosis: [{ field: "date_of_diagnosis", value: "" }, { field: "date_of_symptom_onset", value: "2019-09-01" }], time_since_onset: [{ field: "date_of_symptom_onset", value: "2019-09-01" }] } }

console.log(check(object));

object.params.time_to_diagnosis[0].value= "foo";
console.log(check(object));

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.