I want to overwrite(=update/correction elements) elements from the first row to the third row.

Joined
Jul 9, 2023
Messages
25
Reaction score
0
Code:
                                $.each(array, function (idx, item)
                                {
                                    var layout1 = [];

                                    $.each(item, function (idx2, item2)
                                    {
                                            layout1.splice(layout1.length, 0, item2);                                    
                                    });

                                    searchData.push(layout1);

                                });


I want to overwrite(=update/correction elements) elements from the 0th Row to the 3rd Row.
The code below is a push, so it is added to the end.
I want to overwrite(=update/correction elements) elements from the 0th row to the 3rd Row.

*Caution
Paste :::::: The original array is 7 rows and 5 columns.
(0,0) (0,1) (0,2) (0,3) (0,4) (0,5)
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5)

Copy :::::: The copy array is 3 rows and 5 columns.
(0,0) (0,1) (0,2) (0,3) (0,4) (0,5)
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
Code:
                                $.each(array, function (idx, item)
                                {
                                    var layout1 = [];

                                    $.each(item, function (idx2, item2)
                                    {
                                            searchData.splice(idx2, 1, item2);
                                    });                                                                       

                                });
 
Last edited:
Joined
Jul 4, 2023
Messages
411
Reaction score
47
The original array is 7 rows and 5 columns

BTW, array is 7 rows and 6 columns

...1......2.......3......4.......5......6
(0,0) (0,1) (0,2) (0,3) (0,4) (0,5)


in that way is 1D array
JavaScript:
const arr = [
  [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
  [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
  [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
  [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
  [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5],
  [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5],
  [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5]
];

that mean you can use e.g. slice
JavaScript:
const arr = [
  [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
  [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
  [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
  [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
  [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5],
  [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5],
  [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5]
];

let row = 3;
let col = 6;

const arr_copy_3r_6c = arr.slice(0, (row * col));
console.log(arr_copy_3r_6c);

let arr_2_string = '';
for (let idx in arr_copy_3r_6c) {
  if (idx % col == 0 && idx > 0) arr_2_string += '\n';
  arr_2_string += '[' + arr_copy_3r_6c[idx][0] + ',' + arr_copy_3r_6c[idx][1] + '] ';
}
console.log(arr_2_string);


overwrite=update
e.g in 2nd row 4th column

JavaScript:
function showAsRowAndColumn(arr_2_show, col) {
  let show = '';
  for (let idx in arr_2_show) {
    if (idx % col == 0 && idx > 0) show += '\n';
    show += `[ ${arr_2_show[idx][0]},${arr_2_show[idx][1]} ] `;
  }
  console.log(show);
}

const arr = [
  [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
  [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
  [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
  [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
  [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5],
  [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5],
  [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5]
];

const COLUMN = 6;
let row, col, idx;

row = 3;
col = 6;
const arr_copy_3r_6c = arr.slice(0, (row * col));
console.log(arr_copy_3r_6c);

showAsRowAndColumn(arr_copy_3r_6c, COLUMN);

// 2nd row 4th column
row = 2;
col = 4;
idx = (row * COLUMN) - ((COLUMN - col) + 1);
arr_copy_3r_6c[idx][0] = 9;
arr_copy_3r_6c[idx][1] = 9;

showAsRowAndColumn(arr_copy_3r_6c, COLUMN);
console.log(arr_copy_3r_6c[idx]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,709
Latest member
DRISenaida

Latest Threads

Top