Reputation: 385
I'm not a dev/programmer, I'm an accountant trying to make things easier for me, so sorry if it seems too basic or stupid.
I have an Array (fData) which has length 14 and in which index/item it has another array with length 9.
I'm iterating with it and when it meets the condition I need, I'm trying to copy each of the corresponding 9 length array elements to a newly created Array (fRetention).
I have researched a lot and used so many approaches like Array.prototype.push/apply, Push, Concat, Slice and so on.
The closest I got to what I need was using Slice, but rather than create a new index/item to the new array, it ends overwriting the fData Array and creating an Array inside an Array as per the screenshot below.
Thanks for the help!
let fRetention = [];
let countret = 0;
for (var row = 0; row <= fData.length -1; row++) { //parse Array content
if (row > 0 && row < fData.length -1) { //check for retention
if (fData[row][0] == fData[row - 1][0] && fData[row][2] == fData[row - 1][2] && fData[row][1].substr(0,1) == '2' ) {
fRetention[countret] = fData.slice(row, row + 1);
countret ++;
Upvotes: 0
Views: 2186
Reputation: 385
I tried many approaches as said, but I found a solution using this
fRetention.push (fData[row].slice(0));
Upvotes: 0
Reputation: 2570
if you want to copy just one element of fData to fRetention each time just use the index number so instead of doing this fRetention[countret] = fData.slice(row, row + 1); try
fRetention[countret] = fData[row];
if you want to get a clone of that element try :
fRetention[countret] = [...fData[row]];
Upvotes: 1