2

I have the following object:

productDetails: {
    cislife: [],
    prime: []
  }

I want to return null instead of empty array if there aren't any values available.

e.g: I want to check if any of the arrays are empty and return null as the key value, either cislife or prime. If prime is populated and cislife is not then i would return the values for prime and null for cislife

Any ideas?

3
  • Please precise the problem. Where's the loop? Which array are you talking about - the cislife or prime? Commented Jul 25, 2019 at 20:24
  • 1
    Okay. I want to check if any of the arrays are empty and return null, either cislife or prime. If prime is populated and cislife is not then i would return the values for prime and null for cislife. Commented Jul 25, 2019 at 20:26
  • 3
    return... where? You have a function? Could you edit your question, and share our attempt, give the output for several border cases? Commented Jul 25, 2019 at 20:29

4 Answers 4

4

You could do something like this where you use Array.prototype.reduce and build out an object literal, replacing [] with null where appropriate:

const productDetails = {
  cislife: [],
  prime: [],
  nonEmptyExample: [1, 2, 3],
  nonArrayExample: 'a string!'
};

const emptyArraysToNulls = o => {
  return Object.entries(productDetails).reduce((accum, entry) => {
    const [key, val] = entry;
    if (Array.isArray(val)) {
      accum[key] = val.length === 0 ? null : val;
    } else {
      accum[key] = val;
    }

    return accum;
  }, {});
};

console.log(emptyArraysToNulls(productDetails));

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

3 Comments

What if the object has other keys that are not arrays? like { cislife: [], prime: [], date: 'may 1, 2019' }
Good point! - i modified the code to use Array.isArray to check and only act on array values - all other types will just be copied to the new object.
Yea this is perfect as well
2

This is a way to produce the desired array without mutating the original one:

const initialObject = {
  cislife: [],
  prime: [],
  nonArrayField: "I'm not an array, leave me alone!"
};

const resultObject = {
  ...initialObject,
  ...Object.keys(initialObject)
    // filter out keys that do not belong to empty arrays
    .filter(key => Array.isArray(initialObject[key]) && !initialObject[key].length)
    // replace empty arrays with nulls
    .reduce((acc, key) => ({ ...acc, [key]: null }), {}),
};

console.log(resultObject);

1 Comment

Brilliant Answer
0

If you can edit the object you can use getters

var productDetails = {
  _cislife: [],
  get cislife() {
    return this._cislife.length ? this._cislife : null
  },
  _prime: [],
  get prime() {
    return this.prime.length ? this.prime : null
  },
}

console.log(productDetails.cislife)

1 Comment

Nice idea. But a more generic solution would be preferred
0

You can use getter methods, like below:

var productDetails = {
    _cislife: [],
    _prime: [],

    get cislife() {
        return this._cislife.length == 0 ? null : this._cislife;
    },

    get prime() {
        return this._prime.length == 0 ? null : this._prime;
    }
}

console.log(productDetails.cislife); // null
console.log(productDetails.prime); // null

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.