【问题标题】:Find the duplicates in array and add the value of that indexes of another array查找数组中的重复项并添加另一个数组的索引值
【发布时间】:2021-12-31 08:09:42
【问题描述】:

我有三个数组

product_id = [];
product_category_id =[];
product_amount =[];

在此数组是动态的,因为来自用户选择。但我想要做的是,如果产品 ID 相同,我想添加产品数量并使用特定的 product_id、category_id 和总金额计算总数。

例如如果

product_id = [4,5,1,3,5];
product_category_id =[2,2,1,3,2];
product_amount =[3000,4000,5000,5000,4000];

这是我想要的输出

product_id || product_category_id || product_new_amount
4                 2                    3000
5                 2                    8000      
1                 1                    5000
3                 3                    5000

【问题讨论】:

标签: javascript arrays for-loop duplicates


【解决方案1】:

有很多方法可以实现您的目标,具体取决于您希望如何使用数据。如果数组总是相同的长度,基本上你需要做的是创建一个新的对象数组,检查product_id 和product_category_id 组合是否已经被添加。如果有,则将产品数量添加到该条目中,如果没有,则为数据创建一个新条目。

一旦数据被组织成一个数组,你就可以用它做任何你想做的事情。我用数据填写了一个表格作为一个简单的演示。

希望这会为您指明正确的方向 ? 如果您对我的代码有任何疑问,请询问。

const product_id = [4,5,1,3,5];
const product_category_id = [2,2,1,3,2];
const product_amount = [3000,4000,5000,5000,4000];
const productData = [];
//format data
for (let i = 0; i < product_id.length;  i++) {
  const pid = product_id[i];
  const pcid = product_category_id[i];
  const prodAmt = product_amount[i];
  const target = productData.find(data => data.pid == pid && data.pcid == pcid);
  if (target) target.prodAmt += prodAmt;
  else productData.push({pid, pcid, prodAmt});
}
//create rows
const table = document.querySelector("table");
for (const data of productData) {
  const tr = document.createElement("tr");
  const values = Object.values(data);
  for (const value of values) {
    const td = document.createElement("td");
    td.innerText = value;
    tr.appendChild(td);
  }
  table.appendChild(tr);
}
<table>
  <tr>
    <th>product_id</th>
    <th>product_category_id</th>
    <th>product_new_amount</th>
  </tr>
</table>

【讨论】:

  • thankyou.it 工作
【解决方案2】:

此解决方案可以帮助您。请试试这个:

const product_id = [4,5,1,3,5];
const product_category_id =[2,2,1,3,2];
const product_amount =[3000,4000,5000,5000,4000];

function find_duplicate_in_array(array){
const count = {}; const category_id = {}; const amount = {};
const result = [];

array.forEach((item,index) => {
    if (count[item]) {
       count[item] +=1;
       amount[item] += product_amount[index];
       return
    }
    count[item] = 1; 
    amount[item] = product_amount[index];
    category_id[item] = product_category_id[index];
});

for(let prop in count){
    result.push({product_id:prop,product_category_id:category_id[prop],product_new_amount:amount[prop]})
}

console.log('result---->', result);
return result;
}
find_duplicate_in_array(product_id);

【讨论】:

    【解决方案3】:

    首先,从数组中创建对象。

    product_id = [4,5,1,3,5];
    product_category_id =[2,2,1,3,2];
    product_amount =[3000,4000,5000,5000,4000];
    
    function Item(i,ci,a) {
    this.id=i;
    this.category_id=ci;
    this.amount=a;
    }
    
    obj=[];
    product_id.forEach((v,k)=>{obj.push(new Item(product_id[k],product_category_id[k],product_amount[k]))});
    document.write(JSON.stringify(obj),'<br>');
    
    document.write('<br>');
    

    然后创建两个Map,一个用于id:amount 配对,另一个用于id:category 关系。

    遍历数量,如果键已经存在,则将新值添加到其中。如果没有,请使用新的键和相应的值更新 both Map。

    m=new Map(); m2=new Map();
    obj.forEach(o=>{
    if (m.has(o.id)) m.set(o.id,m.get(o.id)+o.amount);
    else {
    m.set(o.id,o.amount);
    m2.set(o.id,o.category_id);
    }
    });
    

    最后,显示数据,注意使用正确的Map。

    m.forEach((v,k)=>document.write(k+', '+m2.get(k)+', '+v,'<br>'));
    

    把它放在一起:

    product_id = [4,5,1,3,5];
    product_category_id =[2,2,1,3,2];
    product_amount =[3000,4000,5000,5000,4000];
    
    function Item(i,ci,a) {
    this.id=i;
    this.category_id=ci;
    this.amount=a;
    }
    
    obj=[];
    product_id.forEach((v,k)=>{obj.push(new Item(product_id[k],product_category_id[k],product_amount[k]))});
    document.write(JSON.stringify(obj),'<br>');
    
    document.write('<br>');
    
    m=new Map(); m2=new Map();
    obj.forEach(o=>{
    if (m.has(o.id)) m.set(o.id,m.get(o.id)+o.amount);
    else {
    m.set(o.id,o.amount);
    m2.set(o.id,o.category_id);
    }
    });
    
    m.forEach((v,k)=>document.write(k+', '+m2.get(k)+', '+v,'<br>'));

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 1970-01-01
      • 2021-06-21
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 2015-02-21
      相关资源
      最近更新 更多