How do you reassign the value of an item in an array, that's within an array?

​

const clickedspaces = [
        [0, 0],
        [0, 0, 0],
        [0, 0],
        [0, 0, 0],
        [0, 0],
        ];
      
      clickedspaces[0[0]] = 1;
      console.log(clickedspaces);


// What shows up in the console:
(5) [Array(2), Array(3), Array(2), Array(3), Array(2), undefined: 1]
0: (2) [0, 0]
1: (3) [0, 0, 0]
2: (2) [0, 0]
3: (3) [0, 0, 0]
4: (2) [0, 0]
undefined: 1
length: 5
__proto__: Array(0)

I'm trying to assign the value of the first integer there to 1.

clickedspaces[0]

The above code would target the first array [0, 0] but I don't know how you might target the first item in this array. Thanks.

Posted Oct 4, 2020, 12:33 AM UTC

311
redsandsfort•

clickedspaces[0][0] = 1;

14
Professor155OP•

Thank you very much!

1
eggtart_prince•

You're no fun.

-6
redsandsfort•

why?

6
eggtart_prince•

Because giving straight answers doesn't teach people anything and it's boring.

-6
albedoa•

Your answer moves the OP toward working code, which helps OP in the short term, but u/redsandfort's answer quickly and clearly demonstrates the problem with clickedspaces[0[0]] = 1. What you see as giving a straight answer, I see as showing (i.e. teaching).

5
Professor155OP•

I don't know why the syntax to do what I needed is that way, but now I do know how to do it at least.

Lol I like how you said it's boring. I find both approaches helpful where I have some basic knowledge on the topic. Thank you as well!

1
patopitaluga•

The answers that the other user gave you is right, but allow me to give you a small tip that you'll find useful too. You can put objects into the array. I asume that you're keeping track of points in a matrix and usually we name the x axis first and then the y axis but, is you're working with multiple dimensions or other type of data you'll forget which one was which one in the array. So: clickedspaces = [{ x: 10, y: 5 }, { x: 3, y: 7 }]

Thay way you don't have to refer to clickedspaces[0][1] for example, but to clickedspaces[0].y and that's easier to remember, to understand and to talk about it with a colleague.

5
Professor155OP•

Ohh thank you. I'm trying to make a grid layout like the ones used on android passwords (except for a game). Instead of connecting the dots by drawing, I just figured I'd connect them by clicking on an element in between each dot, and use this array to track what's been clicked. Don't know if I can do that yet but ye. I don't need diagonal connections either. Idk if what you suggested might help me here but I appreciate it. Imma save this comment.

I'm in hs as well. The assignment was kind of just handed to us without direction so I'm just trying to survive lol

1
[deleted]•

[deleted]

1
casperCantEat•

As patopitaluga said for readability, I reference it to create this barebone code, as others could say, that accesses and modifies an element array in a readable and scalable way, if you will. Thank you, patopitaluga.

Anyone, please advise me about the solution. Thank you.

​

    function modifyMatrix(mainArr) {
        // It contains an element array's 
        // function to set/modify its certain element.
        let arrFns = {}; 
    
        // Creation of an element array's function 
        // to set/modify its certain element.
        for (let i = 0; i < mainArr.length; i++) {
            arrFns[`arr${i}`] = function (idx, elm) {
                let elementArr = mainArr[i];
                elementArr[idx] = elm;
            }
        }
        
        return arrFns;
    }

Example with your case:

   let clickedspaces = [
        [0, 0],
        [0, 0, 0],
        [0, 0],
        [0, 0, 0],
        [0, 0],
    ];
    
    let arrAddOnFns = modifyMatrix(clickedspaces);
    arrAddOnFns.arr1(1, 1); 
    
 /* (5) [Array(2), Array(3), Array(2), Array(3), Array(2)]
    0: (2) [0, 0]
    1: (3) [0, 1, 0]
    2: (2) [0, 0]
    3: (3) [0, 0, 0] 
    4: (2) [0, 0] 
 */
1
eggtart_prince•

Try declaring variables, it might help.

const firstItemOfClickspaces = clickspaces[0];

Now do the same thing to firstItemOfClickspaces to get the first item of the first item of clickspaces.

-3