Solved

find null values in multidimensional associative array

Posted on 2012-04-04
11
261 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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 …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now