www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Complex Array / Object Filtering

  1. #1
    Join Date
    May 2008
    Posts
    30

    resolved [RESOLVED] Complex Array / Object Filtering

    I have an array filled with objects.

    Code:
    var myArray = [{'el' : el, 'errorCode' : errorCode }, {'el' : el, 'errorCode' : errorCode }, {'el' : el, 'errorCode' : errorCode }];
    The array is built programatically, with el being an element on a page and errorCode being an error code I generate based on arbitrary logic. What I'd like to do is cycle through the array and compare the object.el to see if it has any duplicates. If so, take the error message and combine it (either make that part of the object an array or just concat - I can deal with that) with the other error codes.

    Something to this effect:
    Code:
    myArray.each(function(n){
      n.el;
      n.errorCode;
      //I know that code does nothing, but that's the way I access the object
      //just giving example....
    });
    I know prototype has things like .any, .collect, etc., but I am stumped as to how I can do this.

    My whole goal is to make sure I only have unique elements in my array and have the errorcodes be an array or a - separated string.

    ....so the end result will be something to this effect:
    Code:
      myNewArray = [{'el' : uniqueObject, 'errorCodes' : ['This is wrong.','That is wrong.', 'I love oranges.'] },  {'el' : el2, 'errorCode' :  'This element is ugly!'}];
    again, coming from:
    Code:
    var myArray = [{'el' : el1, 'errorCode' : 'This is wrong.' }, {'el' : el1, 'errorCode' : 'That is wrong.'}, {'el1' : el, 'errorCode' :  'I love oranges.'}, {'el' : el2, 'errorCode' :  'This element is ugly!'}];
    I hope this makes sense.....

  2. #2
    Join Date
    Jul 2009
    Posts
    45
    You're doing a good job so far.
    You've planned most of it out and now you need to program it.
    Keep on researching it by yourself, so you can learn.

    However, your input and output doesn't match.
    'el1' isn't defined in the output.

    Hint:
    if( storage[ item.name ] ){
    storage[ item.name ].property += item.property;
    }
    Help me out by telling others about my jquery plugin.
    http://code.google.com/p/jquizme/

    Learn Javascript, not java, here. Take a 3 days.
    http://www.w3schools.com/JS/default.asp

  3. #3
    Join Date
    May 2008
    Posts
    30
    Nice...thanks for the tip. ..and yeah I ended up just putting that 1 in the wrong part. I was just frustrated. I ended up doing something like that for my solution. I took care of it, when I created the array, rather than afterwards.

    This was my final add function:
    Code:
    markForError : function(el, errorCode){
    		if(previousElement = this.errors.find(function(obj){ return obj.el == el}) ){	
    			previousElement.errorCode.push(errorCode);
    		} else {
    			this.errors.push({'el' : el, 'errorCode' : [errorCode] });
    		}
    		
    	}
    Last edited by polygone; 07-13-2009 at 02:54 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles