0

I have two objects, the first one:

prods = [
        {
            'id':1,
            'category_ID' : 26,
            'name':'prod1',
            'descripcion' : 'sadasdsad',
        },
        {
            'id':2,
            'category_ID' : 26,
            'name':'prod2',
            'descripcion' : '21312d',
        },
        {
            'id':3,
            'category_ID' : 6,
            'name':'prod3',
            'descripcion' : 'ergrd',
        },
        {
            'id':4,
            'category_ID' : 12,
            'name':'prod4',
            'descripcion' : 'wwwwwwww',
        }

    ]

And the second one, with its elements ordered randomly

orders = [
        {
            'id':2,
            'category_ID' : 26,
            'name':'producto2',
            'descripcion' : '21312d',
            'units': 1,
            'isSelected' : 1
        },
        {
            'id':4,
            'category_ID' : 12,
            'name':'producto4',
            'descripcion' : 'wwwwwwww',
            'units': 21,
            'isSelected' : 1
        },
        {
            'id':1,
            'category_ID' : 26,
            'name':'producto1',
            'descripcion' : 'sadasdsad',
            'units': 34,
            'isSelected' : 1
        }

    ]

I'm trying to update the elements of the first object with the ones of the second matching id's and adding new propierties, so the result would be

prods = [
        {
            'id':1,
            'category_ID' : 26,
            'name':'prod1',
            'descripcion' : 'sadasdsad',
            'units': 34,
            'isSelected' : 1
        },
        {
            'id':2,
            'category_ID' : 26,
            'name':'prod2',
            'descripcion' : '21312d',
            'units': 1,
            'isSelected' : 1
        },
        {
            'id':3,
            'category_ID' : 6,
            'name':'prod3',
            'descripcion' : 'ergrd',
        },
        {
            'id':4,
            'category_ID' : 12,
            'name':'prod4',
            'descripcion' : 'wwwwwwww',
            'units': 21,
            'isSelected' : 1
        }

    ]

I'm getting dizzy in a sea of loops... with no result... any clues?

Diego Vega
  • 223
  • 6
  • 16

2 Answers2

4

Try something like this:

function merge(products, orders){

    for(var i = 0; i < products.length; i++){
        var prod = products[i];
        for(var j = 0; j < orders.length; j++){
            var order = orders[j];
            if(order.id == prod.id){
                for(var key in order){
                    prod[key] = order[key];
                }
            }
        }
    }
}

You could then use the function like so;

merge(prods, orders);
devqon
  • 13,818
  • 2
  • 30
  • 45
  • 3
    Using `for/in` to iterate over arrays is usually bad practice; consider using `for (;;;)` loops instead; see http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – Matt Feb 20 '15 at 13:55
  • Good point :) in my cases it didn't really mather, but you are correct that it usually is a bad practice – devqon Feb 20 '15 at 14:21
0

If you don't mind using jquery, you could do the following :

// Go through prods (destination) array
var prods = $.grep(prods, function( prod ) {

    // Go through orders (source) array
    var result = $.grep(orders, function(order){ 

        // Match id on both arrays, if same extend ( copy over ) object
        if (order.id === prod.id) {
            $.extend(prod, order);

            // Returning true keeps this item in the array ( If nothing or false is returned, grep won't keep it in result )
            return true;
        }
        // else .. don't keep the item ? ( Your choice .. don't know how you want to manage it )
    });

    // Return all items ( Don't filter 'prods' items and therefore return all )
    return true;
});

// Print for testing
var _prods = JSON.stringify(prods)

That's all.

DarkUrse
  • 2,084
  • 3
  • 25
  • 33