Remove Nullish values from multidimensional array?

I have an array res with some nullish values, and I have a function remove that is supposed return an array with nulls and undefined removed, but I can’t get it to work on my array. I’ve seen plenty of answers on this sort of thing, in fact my remove function originated from one of them, but I can’t seem to get it to work.

res =    
[
{
    "1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087a696f6f71486b60696578266b67">[email protected]</a>",
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06617469766346656e676b76286569">[email protected]</a>",
        null,
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234b424f5363404b424e530d404c">[email protected]</a>"
    ]
},
{
    "149Lmt-gweagewfrthjregjiojoinONEDOnonao": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a382b2d2d330a29222b273a642925">[email protected]</a>"
    ]
},
{
    "JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f081306121e113f1c171e120f511c10">[email protected]</a>",
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acdecdcbcbd5eccfc4cdc1dc82cfc3">[email protected]</a>"
    ]
},
{
    "1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [
        null,
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e09281878799a08388818d90ce838f">[email protected]</a>"
    ]
},
{
    "FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ceefdfbfbe5dcfff4fdf1ecb2fff3">[email protected]</a>"
    ]
},
{
    "fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea988b8d8d93aa89828b879ac48985">[email protected]</a>"
    ]
},
{
    "FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": [
        "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebcafa9a9b78eada6afa3bee0ada1">[email protected]</a>"
    ]
}
]

var remove = function (array) {
var result = [];

array.forEach(function (item) {
  if (Array.isArray(item) && item.length!=0) {
    // Item is a nested array, go one level deeper recursively
    result.push(remove(item));
  }
  else if (typeof item !== null) {
    result.push(item);
  }
});

return result;
};

console.log(remove(res));

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here is a solution without recursion that will work for the nesting level given in example. Also will work if a single array element has multiple key value pairs.

let res =[{"1yKKftO0iOyvsacrW1mEr-FylurU8-fwaefewafw": ["[email protected]","[email protected]",null,"[email protected]"]},{"149Lmt-gweagewfrthjregjiojoinONEDOnonao": ["[email protected]"]},{"JG043AHF0GJA0EWJIFJO00WIJF-UffFWEAk8QRg4": ["[email protected]","[email protected]"]},{"1u-Frw5I4agI-FWKE0AFJ0WEJG0JEFDALKFEWA-ns": [undefined,"[email protected]"]},{"FAWGETAIODIOFAIJDSOIFJWEOFijewaofifejowef": ["[email protected]"]},{"fwaejf0JF0EWJIJFFJMojfeoijfewJEJFI0i0fje": ["[email protected]"]},{"FJ09Ejf093ejfie0jfeiJFEJF0IWJFEIJFOEJWow": ["[email protected]",null]}]

var remove = function (array) {

return array.map(function (item) {
  let x = Object.entries(item).map((y) => {
    return [y[0],y[1].filter((z)=> z!==undefined && z!==null)]
  })
  return Object.fromEntries(x)
});
};

console.log(remove(res));

Method 2

If you want to remove nulls as well as undefineds, you probably want to replace else if (typeof item !== null) with else if (typeof item != null)

Method 3

What’s happening

The elements of res are Objects.
Notice that in the nested function call of remove

result.push(remove(item));

the item being passed are elements of res thus not an array. So when remove(item) is called the check Array.isArray(item) fails and nothing is sorted out.
To get the inner array make add this line.

var values = Object.values(item)

Now handle the cases of item being null, Object and Array.

Solution

Here’s my Attempt at the solution. (I hope you don’t mind ES6)
This does work on this particular (not sure about other cases)

const remove = (item) => {
  if (item) {
    console.log('not null', item)
    if (Array.isArray(item)) {
      const result = []
      for (let elem of item) {
        const cleanedElem = remove(elem)
        // Maybe use Nullish coalescing operator ?
        if (cleanedElem !== null && cleanedElem !== undefined)
          result.push(cleanedElem)
      }
      return result
    } else if (typeof item === 'string' || typeof item === 'number') {
      return item
    } else if (item) {
      const result = {}
      for (let pair of Object.entries(item)) {
        const [key, value] = pair
        const cleanedValue = remove(value)
        // Maybe use Nullish coalescing operator ?
        if (cleanedValue !== null && cleanedValue !== undefined)
          result[key] = remove(cleanedValue)
      }
      return result
    }
  }
}

cleansed = remove(res)
console.log(cleansed);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x