Solved

find null values in multidimensional associative array

Posted on 2012-04-04
11
264 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

785 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