KennethC KennethC - 5 months ago 11
Javascript Question

Merging Associative arrays, get the latest, and remove a duplicate id

I want to merge 2 associative array. when the user press the save and next button,

it should store all the data for that specific country. If the

country_id
already exists, it should replace all of its value with the latest update of the user.

window.main_array = []; // all of the data of sub_array will be transferred here.


// when a user clicks a button

// fetch all the input
sub_array = {
'country_id':country_id, // <- should be unique

'countryorigin':countryorigin, // <- should be updated
'marketingbudget':marketingbudget, // <- should be updated
'distributor':distributor, // <- should be updated
'salesrep':salesrep, // <- should be updated
'commission':commission, // <- should be updated
'retainer':retainer, // <- should be updated
'expense':expense, // <- should be updated
'buy_sell':buy_sell, // <- should be updated
'instore':instore, // <- should be updated
'merchandiser':merchandiser, // <- should be updated
'can_sell':can_sell // <- should be updated
};

// the main_array should have a unique country_id, and get the replace the old one with the latest if user updates a value for that country

if(main_array.length <= 0){
// just concat the two arrays if there are no data yet in the main_array
main_array = main_array.concat(sub_array);
}else{
// ???
// should only get the latest input for the selected country'
// replace the old data with the new one
}

// end of click event

Answer

See code:

Note: It is written with assumption that you have already ran the form for awhile and main_array has already got some inputs for comparing.

var main_array = [
  {
    'country_id':"country_0", // <- should be unique

    'countryorigin':"Singapore",            // <- should be updated
    'marketingbudget':1000,        // <- should be updated
    'distributor':"lll",                // <- should be updated
    'salesrep':"tan",                      // <- should be updated
    'commission':"900",                  // <- should be updated
    'retainer':"_helloworld__",                      // <- should be updated
    'expense':99,                        // <- should be updated
    'buy_sell':true,                      // <- should be updated
    'instore':false,                        // <- should be updated
    'merchandiser':"hehe",              // <- should be updated
    'can_sell':false                       // <- should be updated
  },
  {
    'country_id':"country_1", // <- should be unique

    'countryorigin':"australia",            // <- should be updated
    'marketingbudget':1000,        // <- should be updated
    'distributor':"ddd",                // <- should be updated
    'salesrep':"smith",                      // <- should be updated
    'commission':"200",                  // <- should be updated
    'retainer':"_helloworld__",                      // <- should be updated
    'expense':50,                        // <- should be updated
    'buy_sell':true,                      // <- should be updated
    'instore':false,                        // <- should be updated
    'merchandiser':"hehe",              // <- should be updated
    'can_sell':false                       // <- should be updated
  },
  {
    'country_id':"country_2", // <- should be unique

    'countryorigin':"Malaysia",            // <- should be updated
    'marketingbudget':600,        // <- should be updated
    'distributor':"ooo",                // <- should be updated
    'salesrep':"robot",                      // <- should be updated
    'commission':"9005",                  // <- should be updated
    'retainer':"_ddddd__",                      // <- should be updated
    'expense':990,                        // <- should be updated
    'buy_sell':false,                      // <- should be updated
    'instore':true,                        // <- should be updated
    'merchandiser':"hehe",              // <- should be updated
    'can_sell':false                       // <- should be updated
  },
]; // all of the data of sub_array will be transferred here.


// when a user clicks a button

// fetch all the input
var sub_array = {
  'country_id':"country_1", // <- should be unique

  'countryorigin':"australia",            // <- should be updated
  'marketingbudget':5000,        // <- should be updated
  'distributor':"xyz",                // <- should be updated
  'salesrep':"john",                      // <- should be updated
  'commission':"100",                  // <- should be updated
  'retainer':"myer",                      // <- should be updated
  'expense':50,                        // <- should be updated
  'buy_sell':true,                      // <- should be updated
  'instore':true,                        // <- should be updated
  'merchandiser':"haha",              // <- should be updated
  'can_sell':false                       // <- should be updated
};

// the main_array should have a unique country_id, and get the replace the old one with the latest if user updates a value for that country

if(main_array.length <= 0){
  // just concat the two arrays if there are no data yet in the main_array
  main_array = main_array.concat(sub_array);
}else{
  main_array = main_array.map(function(country) {
    if (country.country_id === sub_array.country_id) {
      return sub_array;
    }
    return country;
  })
}

You might want to pay particular attention to the else {} clause because that is where the algorithm for solving your problem is.

What the map API does here is, it goes through every single object defined within the main_array list. Then for each iteration an object is returned.

See Map API documentation here.

The map() method creates a new array with the results of calling a provided function on every element in this array.

So for solving your problem I would take the comparer object country.country_id and do a string match to see if it is the same as the sub_array.country_id, if it is the same then return sub_array (overriding) otherwise just return the original country object.

Comments