0

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.

fRetention Array structure

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 ++;
        
augustus182l
  • 375
  • 1
  • 2
  • 12
  • To confirm, you want to filter `fData` and clone data from matching 9-element arrays into a single array of elements (not an array of arrays)? – theusaf Aug 25 '21 at 03:45
  • In the example given, at fData[4], there are 9 matching elements, I would like to copy those to a new array and so on. So in this case, the new Array would receive 4 elements, with 9 matching elements. – augustus182l Aug 25 '21 at 14:15

2 Answers2

1

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]];
pref
  • 1,651
  • 14
  • 24
  • This way the fRetention ends up receiving all the elements from the fData. I only need the matching element, which the first case is fData[4] as fRetention[0] and so on... there should be at the end 4 elements in the array with 9 matching elements inside it. – augustus182l Aug 25 '21 at 14:18
  • that is part of you if (condition) logic, to decide which fData[row] to be copied. I only talked about this line in your code " fRetention[countret] = fData.slice(row, row + 1); " – pref Aug 25 '21 at 14:34
  • Ok, but with this param fRetention[countret] = [...fData[row]]; , where countret = 0, row = 4 . It was not supposed to only add the fifth element of the fData array to the '0' element of the fRetention array? Because this way it basically copied the whole array. – augustus182l Aug 25 '21 at 16:05
0

I tried many approaches as said, but I found a solution using this

fRetention.push (fData[row].slice(0));

Source: For a deep copy of a JavaScript multidimensional array, going one level deep seems sufficient. Is this reliably true?

augustus182l
  • 375
  • 1
  • 2
  • 12