Yamil Duba
Yamil Duba

Reputation: 61

Iterate over shallow copy of nested array in Javascript

I have a nested array:

let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

I need to iterate over the first and second element of every nested array and update the changes on the original array. How do I achieve this? I have tried many options but the results don't update the original array. For example:

let arrayCop = [];
for (let i = 0; i <= 1; i++) {
   for (let j = 0; j <= 1; j++) {
      arrayCop.push(array[i][j]);
   }
}

arrayCop.forEach(...);

Thanks.

This is my full code, I'm trying to build a legal sudoku generator:

let sudoku = [];

function populateSudoku() {
    let array = [];
    while (array.length <= 8) {
        let randomNum = Math.floor(Math.random() * 9 + 1);
        array.push(randomNum);
        if (array.indexOf(randomNum) < array.lastIndexOf(randomNum)) {
            array.pop()
        }
    }
    return array;
}

while (sudoku.length <= 8) {
    sudoku.push(populateSudoku());
}

for (let i = 0; i < sudoku.length; i++) {
    for (let j = 0; j < sudoku.length; j++) {
        sudoku[i].forEach(element => {
            if (sudoku[i].indexOf(element) === sudoku[j].indexOf(element) &&
                (i !== j)) {
                sudoku[j][sudoku[i].indexOf(element)] = 0;
            }
        })
    }
}

let array = [];

for (let i = 0; i <= 2; i++) {
    for (let j = 0; j <= 2; j++) {
        array.push(sudoku[i][j]);
    }
}

array[3] = 452345;

console.log(sudoku);

**

# I did it! #

**

let array = [[1, 2, 3], [7, 4, 1], [2, 4, 3]];

// checks for duplicates just in first and second item of every file
for (let i = 0; i <= 1; i++) {
    for (let j = 0; j <= 2; j++) {
        array[i].forEach((element, index) => {
            if ((i !== j) && index <= 1 &&
            (array[j].indexOf(element) >= 0 && array[j].indexOf(element) <= 1)) {
                array[i][index] = 'x';
            }
        })
    }
}

console.log(array);

Upvotes: 1

Views: 312

Answers (5)

Yamil Duba
Yamil Duba

Reputation: 61

let array = [[1, 2, 3], [7, 4, 1], [2, 4, 3]];

console.log(array);

// checks for duplicates just in first and second item of every file
for (let i = 0; i <= 1; i++) {
    for (let j = 0; j <= 2; j++) {
        array[i].forEach((element, index) => {
            if ((i !== j) && index <= 1 &&
            (array[j].indexOf(element) >= 0 && array[j].indexOf(element) <= 1)) {
                array[i][index] = 'x';
            }
        })
    }
}

console.log(array);

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Use flatMap and destructuring.

let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

let arrayCop = array.flatMap(([first, second]) => [first, second]);

console.log(arrayCop)

Upvotes: 0

Lapys
Lapys

Reputation: 946

ORIGINAL

I need to iterate over the first and second element of every nested array and update the changes on the original array

function iterate(array) {
    array.forEach(function(element, index) {
        console.log('[' + index + "][0]", element[0]);
        console.log('[' + index + "][1]", element[1])
    })
}

Not sure what you mean by update changes to the original array, though...

EDIT

Alright, after looking through other answers, I believe @NinaW got what you were looking for.

function parse(array) {
    array.forEach(function(element) { element.slice(0, 2) })
}

Upvotes: 0

NinaW
NinaW

Reputation: 658

If I understand right, you would like to change the original array to:

[[1, 2], [4, 5], [7, 8]]

If so, this would do it:

array.forEach(element => element.splice(2))

Upvotes: 1

2239559319
2239559319

Reputation: 124

You can use Array.prototype.map function

Upvotes: 0

Related Questions