Solved

find null values in multidimensional associative array

Posted on 2012-04-04
11
267 Views
Last Modified: 2012-06-22
Is there a way to search through an associative array with an unknown number of dimensions and change all the null values to an empty string?

Thanks!
0
Comment
Question by:hibbsusan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37807325
var assArr = new Array();

function iterateOption ( assArr )
{
  while (x in assArr)
  {
      if ( assArr[x] == null )
      {
         assArr[x] = "";
      }
      else if (typeof assArr[x]=="object")
      {
         assArr[x] = iterateOption (assArr[x]);
      }      
  }
}
0
 

Author Comment

by:hibbsusan
ID: 37807753
That's great. works perfect. but I tried to make it work also for undefined. It doesn't seem to work.

I tried it like this

var assArr = new Array();

assArr[0] = null;
assArr[1] = 1;
assArr[2] = null;
assArr[3] = 3;
assArr[5] = 5;

iterateOption(assArr);

function iterateOption ( assArr )
{
  for (x in assArr)
  {
      if ( !assArr[x]  )
      { 
         assArr[x] = "was null or undefined";
      }
      
      else if (typeof assArr[x]=="object")
      {
         assArr[x] = iterateOption (assArr[x]);
      }      
  }
}

console.log (assArr);

Open in new window

¿and got this:
["was null or undefined", 1, "was null or undefined", 3, undefined, 5]

and tried this:

var assArr = new Array();

assArr[0] = null;
assArr[1] = 1;
assArr[2] = null;
assArr[3] = 3;
assArr[5] = 5;

iterateOption(assArr);

function iterateOption ( assArr )
{
  for (x in assArr)
  {
      if ( assArr[x] == null  )
      { 
         assArr[x] = "was null";
      }
      if( typeof assArr[x] === "undefined" ){
         assArr[x] = "was undefined";
      }
      else if (typeof assArr[x]=="object")
      {
         assArr[x] = iterateOption (assArr[x]);
      }      
  }
}

console.log (assArr);

Open in new window


and got this in the console
["was null", 1, "was null", 3, undefined, 5]
¿

Any idea why those don't work?  ><
0
 

Author Comment

by:hibbsusan
ID: 37807782
oh, and i had to change your while-loop to a for-in. the while wouldn't work for me. typo or did i use it incorrectly?
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 37809419
actually, you need to try for undefined like this

if ( assArr[x] == null  )
      {
         assArr[x] = "was null";
      }
if ( assArr[x] == undefined  )
      {
         assArr[x] = "was undefined";
      }

rest should be fine

<<oh, and i had to change your while-loop to a for-in. the while wouldn't work for me. typo or did i use it incorrectly?>>
'While' usually works for me, not sure why it doesn't work for you
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37809552


//Try this:

    alert( denull( {x: null} ).x )


function denull(value) {

    return JSON.parse(JSON.stringify(value), parser)

    function parser(key, value) {

        return (value != null) ? value : "Arrg!"

    }

}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37809651
if (!assArr[x]) // assArr was null, undefined, 0 or ""
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37809934
@mplungjan - if (!assArr[x]) // assArr was null, undefined, 0 or "" - very poor suggestion, not at all helpful

//This is the way to go, as it will work with an object of arbitrary complexity:

    alert( denull( {x: null} ).x )


function denull(value) {

    return JSON.parse(JSON.stringify(value), parser)

    function parser(key, value) {

        return (value != null) ? value : "Arrg!"

    }

}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37809980
Only poor if you cannot use it.

For example it could in theory (since I did not test it) be used like this


function iterateOption ( assArr ) {
  while (x in assArr)  {
      if (!assArr[x]) {
         if (typeof assArr[x] !== "undefined" && assArr[x] !== 0 && (assArr[x] !== "") assArr[x] = "";
      }
      else if (typeof assArr[x]=="object") assArr[x] = iterateOption (assArr[x]);
  }
}
0
 

Author Comment

by:hibbsusan
ID: 37813344
Proculopsis, could you show me how to use this with an example array with some null and undefined values. I'm sorry, I just don't quite understand it..

 mplungjan, again apologies. I cannot get your code to work.

Thanks both!
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37815511

Try this:

var assArr = new Array();

assArr[0] = null;
assArr[1] = 1;
assArr[2] = null;
assArr[3] = 3;
assArr[5] = 5;

assArr = denull( assArr );

alert( assArr[4] );


function denull(value) {

    return JSON.parse(JSON.stringify(value), parser)

    function parser(key, value) {

        return (value != null) ? value : "<null>";

    }

}

Open in new window

0
 

Author Closing Comment

by:hibbsusan
ID: 37823313
brilliant, thanks!
0

Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question