0

Ideally I would like to create an object array in JavaScript like this:

var items = [
  ['test', 2],
  ['test1', 4],
  ['test2', 6]
];

    var test_1 = {};

    for(var i = 0; i < items.length; i++) {

        test_1.items[i][0] = items[i][1];
    }

So, once done I'd like to be able to call

test_1.test which would equal 2.

Is this doable?

2

1 Answer 1

3

You need the bracket notation as property accessor

object.property     // dot notation
object["property"]  // bracket notation

var items = [
  ['test', 2],
  ['test1', 4],
  ['test2', 6]
];

var test_1 = {};

for (var i = 0; i < items.length; i++) {
    test_1[items[i][0]] = items[i][1];
    //    ^           ^
}

console.log(test_1);

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

1 Comment

Thanks Nina! That's great

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.