1

我想要合并2关联数组。当用户按下保存和下一个按钮时,它应该存储该特定国家的所有数据。 如果country_id已经存在,它应该用用户的最新更新替换它的全部值。合并关联数组,获取最新的,并删除重复的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 
+0

我有点困惑,也许我没有得到这个问题。基于你的代码'sub_array'是一个对象,'main_array'是一个空数组,我认为它会包含一些东西?你有一个关于主阵列看起来如何的例子吗? –

+0

因为main_array只是一个容器。子数组的所有数据将在那里传输。 – KennethC

+0

默认情况下main_array不应该有任何值,因为它只是一个容器。在用户填写表单之后,所有的用户输入都将被存储到sub_array中,然后我将它连接到main_array。 – KennethC

回答

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

的地图()方法创建调用此阵列中的每个元件上的 提供的函数的结果的新的数组。

因此,对于解决您的问题,我会采取比较器对象country.country_id,做一个字符串匹配,看它是否是一样的sub_array.country_id,如果是相同的,然后返回sub_array(覆盖),否则只返回原country对象。

+0

嘿,男人,谢谢你,它按预期工作!真棒。 – KennethC