在数组中查找重复项并添加另一个数组的索引值
Find the duplicates in array and add the value of that indexes of another array
我有三个数组
product_id = [];
product_category_id =[];
product_amount =[];
在此数组是动态的,因为来自用户 selection.but 我想做的是,如果产品 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
有多种方法可以实现您的目标,具体取决于您希望如何使用数据。如果数组的长度始终相同,基本上您需要做的是创建一个新的对象数组,检查是否已经添加了 product_id
和 product_category_id
组合。如果有,则将产品金额添加到该条目,如果没有,则为数据创建一个新条目。
一旦数据组织成数组,您就可以用它做任何您想做的事。我用数据填充了一个 table 作为一个简单的演示。
希望这能为您指明正确的方向。如果您对我的代码有任何疑问,请提问。
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>
这个解决方案可以帮到你。请试试这个:
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);
首先,从数组中创建对象。
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>'));
我有三个数组
product_id = [];
product_category_id =[];
product_amount =[];
在此数组是动态的,因为来自用户 selection.but 我想做的是,如果产品 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
有多种方法可以实现您的目标,具体取决于您希望如何使用数据。如果数组的长度始终相同,基本上您需要做的是创建一个新的对象数组,检查是否已经添加了 product_id
和 product_category_id
组合。如果有,则将产品金额添加到该条目,如果没有,则为数据创建一个新条目。
一旦数据组织成数组,您就可以用它做任何您想做的事。我用数据填充了一个 table 作为一个简单的演示。
希望这能为您指明正确的方向。如果您对我的代码有任何疑问,请提问。
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>
这个解决方案可以帮到你。请试试这个:
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);
首先,从数组中创建对象。
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>'));