JavaScript Loop through object and check if field is an empty array and return null loop through object javascript,loop through object keys javascript,loop

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?

Answer:1

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);
Answer:2

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));
Answer:3

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)
Answer:4

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

  1. javascript function that returns a function
  2. javascript function that returns a value
  3. javascript function that returns a promise
  4. javascript function that does nothing
  5. javascript function that returns boolean
  6. javascript function that accepts array
  7. javascript function that returns array
  8. javascript function that returns an object
  9. javascript function that can be suspended and resumed
  10. javascript function that adds numbers
  11. javascript function that returns nothing
  12. javascript function that only runs once
  13. javascript function that calls another function
  14. javascript function that runs on page load
  15. javascript function that executes immediately
  16. javascript function that takes a string
  17. javascript function that adds two numbers
  18. javascript function that returns html
  19. javascript function that takes an array
  20. javascript function that always runs

I tried to load a page inside my phonegape aplication with Jquery .load() but it doesn't work since it's not on a server and it's on local machine. When I'll upload the app on build. phonegape my ...

I tried to load a page inside my phonegape aplication with Jquery .load() but it doesn't work since it's not on a server and it's on local machine. When I'll upload the app on build. phonegape my ...

  1. phonegap load local html file
  2. phonegap load local file

I need to input this code through CKEditor: <ruby>niroj adhikary<rp>(</rp><rt>this is name</rt><rp>)</rp></ruby> CKEditor is not accepting ...

I need to input this code through CKEditor: <ruby>niroj adhikary<rp>(</rp><rt>this is name</rt><rp>)</rp></ruby> CKEditor is not accepting ...

I'm trying to put circles in svg dynamically. In index.html file I created this svg section. <svg id="svgArea" width="500" height="500"></svg> In js file i tried this $('#svgArea')....

I'm trying to put circles in svg dynamically. In index.html file I created this svg section. <svg id="svgArea" width="500" height="500"></svg> In js file i tried this $('#svgArea')....

  1. circle elementary
  2. circle elementary school
  3. circle element html
  4. circle element svg
  5. circle elementary school wichita ks
  6. circle element css
  7. circle elementor
  8. circle elements vector
  9. circle elementary greenwich
  10. circle elementary towanda ks
  11. circle elementary school mt
  12. circle elementary wichita ks
  13. circle elementary school kansas
  14. circle elementary school benton ks
  15. circle elemental symbols
  16. circle element attributes
  17. circle elements radius
  18. circle element design
  19. circle element android
  20. circle element logo