Home > OS >  I have an Array I need to push into new Array with object mapping
I have an Array I need to push into new Array with object mapping

Time:08-13

This is my Array:

[
    {
        "batchno": "B-PI1-1",
        "unitId": 341,
        "productName": "Bar Soap",
        "productProfileId": 3950,
        "qty": 148,
        "returnQty": "20",
        "rate": 10,
        "salesRate": 20,
        "unitName": "PC",
        "gross": 200,
        "net": 200,
        "remarks": "sxzxz"
    },
    {
        "batchno": "B-PI4-1",
        "unitId": 341,
        "productName": "Biscuit",
        "productProfileId": 3951,
        "qty": 700,
        "returnQty": "20",
        "rate": 10,
        "salesRate": 60,
        "unitName": "PC",
        "gross": 200,
        "net": 200,
        "remarks": "zxzxzx"
    }
];

I need to push into a new array, but the last one is doubly pushed:

if (this.primengTableHelper.initialRecords.length > 0) {
    this.primengTableHelper.initialRecords.map((item: any) => {
        console.log('item', item);
        this.singleItem.batchNo = item.batchno;
        this.singleItem.unitId = item.unitId;
        this.singleItem.productName = item.productName;
        this.singleItem.productProfileId = item.productProfileId;
        this.singleItem.qty = item.qty;
        this.singleItem.returnQty = item.returnQty
        this.singleItem.rate = item.rate;
        this.singleItem.salesRate = item.salesRate;
        this.singleItem.unitName = item.unitName;
        this.singleItem.gross = item.net;
        this.singleItem.net = item.net;
        this.singleItem.remarks = item.remarks;
        this.createOrEditDamageStockDto.paymentNO = this.voucherNO;
        this.createOrEditDamageStockDto.invoiceDate = 
            this.maxInvoiceDateFilter.toString();
        this.createOrEditDamageStockDto.damageStockDetailsListDto
            .push(this.singleItem);
    });

CodePudding user response:

I'm not sure that I understand the question. You have an array with two objects. Do you simply want a new array with three objects where the last object is identical to the second one? If so, you could use the ES6 spread operator and do this:

const initialRecords = [
  {
    "batchno": "B-PI1-1",
    "unitId": 341,
    "productName": "Bar Soap",
    "productProfileId": 3950,
    "qty": 148,
    "returnQty": "20",
    "rate": 10,
    "salesRate": 20,
    "unitName": "PC",
    "gross": 200,
    "net": 200,
    "remarks": "sxzxz"
  },
  {
    "batchno": "B-PI4-1",
    "unitId": 341,
    "productName": "Biscuit",
    "productProfileId": 3951,
    "qty": 700,
    "returnQty": "20",
    "rate": 10,
    "salesRate": 60,
    "unitName": "PC",
    "gross": 200,
    "net": 200,
    "remarks": "zxzxzx"
  }
]

const recordsWithDuplicate = [
  ...initialRecords,
  initialRecords[initialRecords.length - 1]
]

You can read about that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_array_literals

I hope this helps :-)

CodePudding user response:

You keep pushing this.singleItem, which is one object, no matter how many iterations are made. In each iteration that single object is mutated, and then pushed again, resulting in multiple references to the same object.

Instead, make sure to create a new object in each iteration. At the same time avoid all those individual assignments, and use spread syntax instead:

        const newItem = {...this.singleItem, ...item};
        // Make other changes to `newItem` properties if needed...
        // ...
        // Push the new object:
        this.createOrEditDamageStockDto.damageStockDetailsListDto
            .push(newItem);

Not your question, but the following lines make no sense in the loop, since they don't depend on the iterated item:

    this.createOrEditDamageStockDto.paymentNO = this.voucherNO;
    this.createOrEditDamageStockDto.invoiceDate = 
        this.maxInvoiceDateFilter.toString();

These statements would always make the same assignment in each iteration. So place them outside that loop.

Note that you shouldn't use .map when you don't use the result. Then just use .forEach. On the other hand, you can use .map to create an array for assigning to this.createOrEditDamageStockDto.damageStockDetailsListDto. Whether that is a good idea in your case, depends on whether that array already exists before the loop, and has content that should stay there.

  • Related