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

标签 javascript jquery arrays multidimensional-array associative-array

我想合并 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 列表中定义的每个对象。然后对于每次迭代都会返回一个对象。

请参阅 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 相同,如果它是相同的,然后返回 sub_array (覆盖),否则只返回原始的 country 对象。

关于javascript - 合并关联数组,获取最新的,并删除重复的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194307/

相关文章:

javascript - 将对对象的引用传递给 jQuery 中的回调函数

Javascript 数组未显示为数组

c 遍历一个数组

javascript - 当 propTypes 验证失败时强制 ReactJS 抛出真正的错误?

javascript - 一个接一个地加载元素——简单的jQuery

javascript - 如何在没有 &lt;script src =""> 的情况下在 Javascript 中包含 jQuery 库

jquery - 根据用户行为使用 jQuery 移除类

python - 如何显示不带引号且以逗号作为分隔符的 NumPy 字符串数组?

javascript - 使用 JSON 从 Ajax 调用获取未定义的数组类型

javascript - 返回数组中的特定对象值