Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

find null values in multidimensional associative array

Posted on 2012-04-04
11
Medium Priority
?
268 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:Gurvinder Pal Singh
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

715 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