1

i'm pretty new to js. i'm sorry if this sounds dumb.I have such an algorithm for javascript. when executed, it loops. Why? What is his mistake? I have such source data enter image description here

let incomesArray = [];
incomesArray[0] = {

    income_id: incomeList[0].id,
    worker_id: incomeList[0].worker_id,
    worker_surname: incomeList[0].worker_surname,
    worker_name: incomeList[0].worker_name,
    incomeList: [
        {
        provider_name: incomeList[0].provider_name,
        product_category: incomeList[0].product_category_name,
        product_name: incomeList[0].product_name,
        product_count: incomeList[0].product_count,
        purchase_price: incomeList[0].purchase_price
        }
    ],
    incomeDate: incomeList[0].date,
    total: incomeList[0].total
}

if(incomesArray.length > 0 ){
  for(let i = 1; i < incomeList.length; i++) {
    for(let j = 0; j < incomesArray.length; j++){
      if(incomeList[i].id == incomesArray[j].id){
          for(let k = 0; k < incomesArray[j].incomeList.length; k++){
                  incomesArray[j].incomeList.push({
                      provider_name: incomeList[i].provider_name,
                      product_category: 
                      incomeList[i].product_category_name,
                      product_name: incomeList[i].product_name,
                      product_count: incomeList[i].product_count,
                      purchase_price: incomeList[i].purchase_price
                   })
               }
      } else if (incomesArray[j].id !== incomeList[i].id) {
        incomesArray.push({
            income_id: incomeList[i].id,
            worker_id: incomeList[i].worker_id,
            worker_surname: incomeList[i].worker_surname,
            worker_name: incomeList[i].worker_name,
            incomeList: [
                {
                provider_name: incomeList[i].provider_name,
                product_category: incomeList[i].product_category_name,
                product_name: incomeList[i].product_name,
                product_count: incomeList[i].product_count,
                purchase_price: incomeList[i].purchase_price
                }
            ],
            incomeDate: incomeList[i].date,
            total: incomeList[i].total
        })
      }
    }
  }                                            
}

I will be grateful for any help or hint.

0

1 Answer 1

1

It's your else part that causes the problem. For every item in incomesArray that has a different id than the item in incomeList you add another entry to incomesArray; while you're iterating over that array. Like this gif:

Extending the track while you're driving on it.

Second, your comparison: if(incomeList[i].id == incomesArray[j].id) but incomesArray[j] has no id, only a income_id, so you'll always run into the else part where you add more items to incomesArray.

I've restructured your code a bit:

const incomesArray = [];

for (const row of incomeList) {
  let target = incomesArray.find(item => item.income_id === row.id);

  if (!target) {
    incomesArray.push(target = {
      income_id: row.id,
      worker_id: row.worker_id,
      worker_surname: row.worker_surname,
      worker_name: row.worker_name,
      incomeList: [],
      incomeDate: row.date,
      total: row.total
    });
  }

  target.incomeList.push({
    provider_name: row.provider_name,
    product_category: row.product_category_name,
    product_name: row.product_name,
    product_count: row.product_count,
    purchase_price: row.purchase_price
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.