0
{
  "top": [{
    "language": "English",
    "value": ""
  }, {
    "language": "German",
    "value": "hASTA"
  }],
  "bottom": [{
    "language": "English",
    "value": "jfgfjg"
  }, {
    "language": "German",
    "value": "fkjhf"
  }],
  "In": "12 am",
  "Out": "3 am",
  "Type": ""
}

Hi guys, I want to remove the keys that have empty values, I tried using filter but that showed me error in google sheets. I want to send only the data to API that has values.

In this case, for

language:german, the value is empty so i Would skip sending top. to the API.

the json to be sent:

{
  "bottom": [{
    "language": "English",
    "value": "jfgfjg"
  }, {
    "language": "German",
    "value": "fkjhf"
  }],
  "In": "12 am",
  "Out": "3 am"
}

code used:

apidata = userdata.filter(function(x) { return x !== "" }); 

Can you please guide me on how to do this?

6
  • 2
    Can you supply the code you tried? i.e. how you tried to use .filter() Commented Jun 29, 2020 at 11:35
  • Why is the entire top array removed from the object? Commented Jun 29, 2020 at 11:38
  • @adiga, API is configured to accept the request only if both the languages are present. Commented Jun 29, 2020 at 11:39
  • @Sean I have added the code that I used, Commented Jun 29, 2020 at 11:41
  • 1
    @MisterJojo The context of the environment in which the code is running gives valuable information. For eg, until recently, Google apps script only supported ES5. Commented Jun 29, 2020 at 14:14

1 Answer 1

4

userdata.filter won't filter the keys of the object.


You can use Array.reduce to create your new object

const json = {
  "top": [{
    "language": "English",
    "value": ""
  }, {
    "language": "German",
    "value": "hASTA"
  }],
  "bottom": [{
    "language": "English",
    "value": "jfgfjg"
  }, {
    "language": "German",
    "value": "fkjhf"
  }],
  "In": "12 am",
  "Out": "3 am",
  "Type": ""
};

// returns true if it contains a falsy value
// this function is recursive
function checkFalsy(ptr) {
   // If we are dealing with an array
  if (ptr instanceof Array) {
    return ptr.some(x => checkFalsy(x));
  }
  
  // If we have a string
  if (typeof ptr === 'string') {
     return ptr.length === 0;
  }
  
  // If we have an object
  if (Object.keys(ptr).length) {
     return Object.keys(ptr).some(y => checkFalsy(ptr[y]));
  }
  
  // anything else
  return !!ptr;
}

const filteredJson = Object.keys(json).reduce((tmp, x) => {
  // If we are dealing with an array
  if (checkFalsy(json[x]) === false) {
    tmp[x] = json[x];
  }

  return tmp;
}, {});

console.log(filteredJson);

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

11 Comments

Could you explain what you did with the const in your answer or in a chat room? I've never seen something like that, good solution.
The const { } purpose is to declare new variables from destructuring the json variable. The purpose here is to extract the keys top and Type from the json object and put all others keys inside of filteredJson. The top: _ syntax is here to rename the variable where we put the top value because top seems to be a reserved variable name
The elements to remove are not known ahead of time, so providing an array of keys to remove won't do anything to meet the need.
The code needs to delete the parent element if one of the child elements has a falsy value.
@AlanWells I've edited my code accordingly with a function checking recursively for falsy values in children
|

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.