【问题标题】:Merging Associative arrays, get the latest, and remove a duplicate id合并关联数组,获取最新的,去掉重复的id
【发布时间】:2016-11-06 18:02:54
【问题描述】:

我想合并 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

【问题讨论】:

  • 我有点困惑,也许我没有得到这个问题。根据您的代码,sub_array 是一个对象,main_array 是一个空数组,我认为它会包含一些东西?你有一个主数组的例子吗?
  • 因为 main_array 只是一个容器。 sub_array 的所有数据都将传输到那里。
  • 默认情况下 main_array 不应该有任何值,因为它只是一个容器。用户填写表格后,所有用户输入将存储到 sub_array,然后我将它连接到 main_array
  • JavaScript 没有“关联数组”。它有数组,也有对象。你把两者混为一谈了。在您的代码中,sub_array 根本不是一个数组,它是一个对象。我建议从您的问题中删除屏幕截图,因为它无助于澄清任何事情。相反,请列出您希望代码生成的实际输出数据,以及它应该是对象还是数组 - 请阅读 JavaScript 中的输出数据之间的区别。
  • 不需要道歉!当您在 PHP 和 JavaScript 等具有相似但不完全相同的特性的语言之间来回切换时,很容易混淆。祝你好运,黑客愉快!

标签: javascript jquery arrays multidimensional-array associative-array


【解决方案1】:

见代码:

注意:假设您已经运行了一段时间并且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

map() 方法使用调用的结果创建一个新数组 在此数组中的每个元素上提供函数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多