mardi 22 mai 2018

Assign object property to value if x = null

I have an Ember computed property that creates an object from an ember model.

In one of the object properties I am trying to evaluate whether an ID exists and in cases where it does not make the property 0 rather than null.

The computed property is as follows (with an illustrative data set):

SpecSortData: computed('customerfr', function() {
  let newArray = [];
  this.get('customerfr').forEach(function(x) {

    function idCheck() {
      if (x.data.speciality_id !== null) {
        return x.data.specialty_id
      } else {
        return 0
      }
    };

    let newData = {
      specialtyID: x.data.specialty_id,
      specialty: x.data.specialty_name,
      customerID: x.data.customer_id,
      tspecID: idCheck()
    }
    newArray.push(newData)
  })
  return newArray
}),

I haven't been able to get the idCheck() function to evaluate properly. The specialty_id field is either a number or null. In cases where it is a number, I would like tspecID to be that number. In cases where it is null I would like tspecID to be 0.

The above function currently adds the speciality_id number to the tspecID property but it does not handle the null cases, they remain as null. If I invert the condition I get all tspecID properties set to 0.

I have tried a few variations of the condition and always end up with either:

  • specID = specialtyID with null cases remaining null
  • tspecID = always 0

I feel like I am making a basic error in the evaluation of function construction but I am stumped.




Aucun commentaire:

Enregistrer un commentaire