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?

2
  • It would be really helpful if you could edit/update your question to include what you have tried. Commented Feb 20, 2015 at 13:51
  • what libraries are you using? or is this just plain js Commented Feb 20, 2015 at 13:52

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Using for/in to iterate over arrays is usually bad practice; consider using for (;;;) loops instead; see stackoverflow.com/questions/500504/…
Good point :) in my cases it didn't really mather, but you are correct that it usually is a bad practice
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.

Comments

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.