SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    From data 1:n to 1:n in reverse order.

    Hi
    From
    Code JavaScript:
    var stack = [
        {user:'Joe',items:[1,2,3]},
        {user:'Bob',items:[3,4,5,6]},
        {user:'Joy',items:[1,4,7,9,10]},
        {user:'Bill',items:[6,8,3]},
        {user:'Elen',items:[9,7,1,4]}
    ];
    to
    Code JavaScript:
    var stack = [
        {id:1,users:['Joe','Joy','Elen']},
        // and go on
    ];

    I've ended up with

    Code JavaScript:
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }
     
    function builder(stack){
        var temp =[],items = [];
        for(var key in stack){
            var len = stack[key].items.length;
            if(len > 0){
                for(var i = 0; i < len; i++){
                    if(!inArray(stack[key].items[i],temp)){
                        temp.push(stack[key].items[i]);
                        items.push({id:stack[key].items[i],users:[stack[key].user]});
                    }
                    else{
                        for(var k in items) {
                            if(items[k].id == stack[key].items[i]){
                               items[k].users.push(stack[key].user);
                            }
                        }
                    }
                }
            }
        }
        return items;
     }
    console.log(builder(stack));

    As usual is there a better way ?


    Bye

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whisher View Post
    As usual is there a better way ?
    This could be a good time to make use of some higher level array methods, such as .forEach() and .some()

    After reordering things to remove the need for the temp array, here's what we get:

    Code javascript:
    function builder(stack) {
        var items = [];
        stack.forEach(function (obj) {
            obj.items.forEach(function (stackItem) {
                var found = items.some(function (item) {
                    if (item.id == stackItem) {
                        item.users.push(obj.user);
                        return true;
                    }
                });
                if (!found) {
                    items.push({
                        id: stackItem,
                        users: [obj.user]
                    });
                }
            });
        });
        return items;
    }

    The above links provide compatibility code too, which you'll need if using them outside of modern web browsers (such as for IE).
    Last edited by paul_wilkins; Mar 12, 2012 at 01:37.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

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