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