我有三個陣列
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
var product_id =$("form[name='id[]']").map(function(){return $(this).value();}).get()
var product_category_id =$("form[name='cat_id[]']").map(function(){return $(this).value();}).get()
var product_amount =$("form[name='amount[]']").map(function(){return $(this).value();}).get()
for(var i=0;i<product_id.length;i ){
if(product_id[i] = product_id[i 1]
{
product_new_amount[i] = product_amount;
}}
以上是我試圖做的,但不知道如何將所有人聚集在一起。
uj5u.com熱心網友回復:
有多種方法可以實作您的目標,具體取決于您希望如何使用資料。如果陣列總是相同的長度,基本上你需要做的是創建一個新的物件陣列,檢查是否已經添加了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>
uj5u.com熱心網友回復:
此解決方案可以幫助您。請試試這個:
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);
uj5u.com熱心網友回復:
首先,從陣列中創建物件。
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關系。
遍歷金額,如果鍵已經存在,則向其添加新值。如果沒有,請使用新鍵和各自的值更新兩者 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>'));
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399085.html
標籤:javascript 数组 for循环 重复
上一篇:傳遞引數以減少函式
