1

I would like set/get value in associative array in 2D array in JavaScript. I used a following source code:

var properties = {
  "isBold": false,
  "isItalic": false,
  "isUnderline": false
};

// Creation of 2D array, 6x6
var listOfProperties = new Array(6);
for (var i = 0; i < listOfProperties.length; i++) {
  listOfProperties[i] = new Array(6);
}

// Population of listOfProperties with properties
for (var row = 0; row < listOfProperties.length; row++) {
  for (var col = 0; col < listOfProperties.length; col++) {
    listOfProperties[row][col] = properties;
  }
}

var property = listOfProperties[2][2];
property.isBold = true; // I would like to populate property isBold
console.log("property > " + property.isBold);

var property = listOfProperties[0][0];
property.isBold = false;
console.log("property > " + property.isBold);

var property = listOfProperties[2][2];
console.log("property > " + property.isBold);

I created 2D array which I populated with properties (associative array). Then I set a true for isBold in listOfProperties[2][2] and false for isBold in listOfProperties[0][0].

But why isBold in listOfProperties[2][2] includes false instead true?

What I do wrong?

2
  • you need to set property.isBold = true; after line var property = listOfProperties[2][2]; Commented Sep 20, 2020 at 8:03
  • Thank you aRvi I wasn't 100% concentrated ... Commented Sep 20, 2020 at 9:00

1 Answer 1

1

Sorry for this question I didn't notice I forgot set a true for listOfProperties[2][2]

Sign up to request clarification or add additional context in comments.

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.