1

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);
3
  • arrayCop.forEach(...); yes, where ... is your attempt to do so Commented Mar 15, 2020 at 2:15
  • 2
    you're not changing the original array in your code, only accessing array[i][j]. So, array will remain as is. Commented Mar 15, 2020 at 2:21
  • arrayCop.forEach(element => {if (arrayCop.indexOf(element) < arrayCop.lastIndexOf(element)) {arrayCop[arrayCop.indexOf(element)] = 'something'}}) Commented Mar 15, 2020 at 2:33

5 Answers 5

1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

No, from the original array I need to create a second flat array containing the first and second items of every file, iterate through this new array and finally that the changes made to this array be reflected on the original one. For example: let arr = [ [1, 2, 3] [4, 5, 6] [7, 8, 9] ]; let newArr = [1, 2, 4, 5, 7, 8]; [do some iteration through newArr here] console.log(arr); // [ ['x', 2, 3] [4, 'x', 6] ['x', 'x', 9] ] Thanks!
0

You can use Array.prototype.map function

Comments

0

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) })
}

Comments

0

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)

Comments

0
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);

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.