Solved

find null values in multidimensional associative array

Posted on 2012-04-04
11
266 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expanding/Collapsing Tree on SharePoint List 29 64
javascript duplicates 9 25
Disabling a Faux Submit Button 4 41
Make qty change inside array as well 1 20
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 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