0

I am trying to push the array into another array. The data is filling in the parent array, but after filling the records, it's overwriting all records in the parent array. Here I have written my code. Please guide me on where I am making a mistake?

Here I am trying to make a model like CSV file header values as a key and CSV file records as a value.

let csvArr: any[] = [];
for (let i = 1; i < csvRecordsArray.length; i++) {
    let curruntRecord = (<string>csvRecordsArray[i]).split(',');
    if (curruntRecord.length == headerLength) {
           let headerRecord: {};
           headerRecord = this.headersRowData
           for(let j = 0; j < this.headersRowData.length; j++){
              headerRecord[this.headersRowData[j]] = curruntRecord[j].trim()
           }
           csvArr.push(csvRecord)
    }
}

HeaderRowData will read the header from the CSV file which is an array type of any[].

csvRecord is getting proper data as per file and loop but while push in csvArr then all existing records in csvArr is overwriting there. So how to resolve this overwriting issue here?

5
  • What sort of data is in csvRecordsArray? Commented Jul 8, 2021 at 16:54
  • 1
    csvArr.push(csvRecord) Should never overwrite. Can’t see the issue in this code. Is this inside a Function maybe? Also the let csvArr: any[] = [];? Because that would simply reset the array everytime you call the function..? Commented Jul 8, 2021 at 16:57
  • @Blunderchips csvRecordsArray is a list of all records of CSV file without 1st line as headers. Commented Jul 8, 2021 at 17:06
  • @MikeOne yes it's inside a function. But that function is calling once only. And whole this process is calling once only there. The only loop is working on records there while we call that function. Commented Jul 8, 2021 at 17:08
  • Can you update your question with data example values / data from: csvRecordsArray, headerLength, headersRowData, csvRecord. I don't even see where csvRecord is defined. Commented Jul 8, 2021 at 17:14

1 Answer 1

1

There is no csvRecord defined anywhere in your code, so I assume, you are actually pushing headerRecord to the array?

If that's the case, the answer is obvious. You are always assigning

headerRecord = this.headersRowData;

which does not create a new object but just assigns headerRecord to be an additional reference to this.headerRowData. Thus, they are both pointing to the same memory location. When you then do

csvArr.push(headerRecord);  //I assume this should be headerRecord

you are just adding an additional reference to the very same object to the array. Ie all elements of the array are in fact pointing to the very same location in memory. In the loops body you are then updating the headerRecord object. And with this update, you are of course also updating all other references, that are pointing to this object. Thus, it seems, it's overwriting many elements. But in reality, it's just changing one element, but all other elements are also pointing to that very same element and therefore reflect these changes as well.

So instead of

let headerRecord: {};
headerRecord = this.headersRowData

just do

let headerRecord = {};
//remove the assignment to this.headerRowData

// update the object here

csvArr.push(headerRecord);

This will create and add a new object in every iteration, which is not related to the other objects ...

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.