0

I have this array in javascript: you can see it here

 Array
(
    [2012-10-01] => Array
        (
            [1] => 1
            [2] => 0

...
            [148] => 0
            [149] => 0
            [150] => 1
        )

[2012-10-02] => Array
        (
            [1] => 0
            [2] => 1

...
            [148] => 0
            [149] => 1
            [150] => 0
        )

[2012-10-03] => Array
        (
            [1] => 1
            [2] => 0

...
            [148] => 0
            [149] => 0
            [150] => 1
        )
..............

to reduce it, I want to keep only items whose have ones and omit the items with zeros. like this

    Array
(
    [2012-10-01] => Array
        (
            [23] => 1
            [64] => 1
            [70] => 1
            [73] => 1
            [76] => 1
            [108] => 1
            [138] => 1
        )

I used underscorejs and this code:

 var new_o={};
    var v = _.each(original_array,function(day,key){
        var arr = {};
        _.map(day,function(item,k){
             if (item){
                 arr[k]=item;
             }
        }) ;

        new_o[key]= arr;
    } )    ;

it works, but I am pretty sure, I didn't get the best of underscore. can anybody suggest a smarter way?

4
  • 1
    this doesn't look like a javascript array. Commented Oct 31, 2012 at 14:22
  • it is, I just print from the browser console Commented Oct 31, 2012 at 14:44
  • what I was trying to say is that you could post the array using javascript syntax to make it easier for potential posters to test your code. Commented Oct 31, 2012 at 15:25
  • please look at the fiddle fiddle.jshell.net/LBa5A/1 (I edited the question above and added a link to the fiddle) Commented Oct 31, 2012 at 15:44

1 Answer 1

2

Don't use _.map if you don't use its return value, that's just a slightly more expensive version of _.each.

As far as simplifying things goes, you're a bit stuck because both Underscore and JavaScript really want to iterate over arrays and you have nested objects (BTW, { } is an object literal in JavaScript, [ ] is an array, they are quite different). Probably the best you can do with your current data structure is to use _.reduce to iterate over the sub-objects while carrying the new sub-object along; something like this:

var new_o = { };
_.each(original, function(day, key) {
    new_o[key] = _(day).reduce(function(memo, item, k) {
        if(item)
            memo[k] = item;
        return memo;
    }, { });
});

Demo: http://jsfiddle.net/ambiguous/FZRV3/

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. Would you please tell me, how would it be if it was arrays? I mean if the structures were arrays.
That would depend on what your array version of the data looked like but it would probably involve _.filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.