合并关联数组,获取最新的,并删除重复的 id

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

我想合并 2 个关联数组。当用户按下保存和下一步按钮时,

它应该存储该特定国家/地区的所有数据。 如果 country_id 已经存在,它应该用用户的最新更新替换它的所有值。

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

见代码:

注意: 假设您已经 运行 表格一段时间并且 main_array 已经有一些用于比较的输入。

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;
  })
}

您可能需要特别注意 else {} 子句,因为这是解决问题的算法所在。

map API 在这里所做的是,它遍历 main_array 列表中定义的每个对象。然后对于每次迭代,一个对象被 returned。

请参阅 Map API 文档 here

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

因此,为了解决您的问题,我将采用比较器对象 country.country_id 并进行字符串匹配以查看它是否与 sub_array.country_id 相同,如果相同则 return sub_array(覆盖)否则只是 return 原始 country 对象。