7

I tried to do this with:

arr[i][j] = 'whatever';

but I get some kind of error "cannot convert to object..."

2 Answers 2

10

I'm going to guess that you haven't initialized a[i] when you try to treat it like an array. If you haven't initialized a[i] to be an array when you say a[i][j], then it will be undefined (or something else that isn't an array or object) and that doesn't know what [j] means, hence your "cannot convert to an object" error. You need something more like this:

var a = [ ];
for(var i = 0; i < 10; ++i) {
    a[i] = [ ];
    for(var j = 0; j < 10; ++j) {
        a[i][j] = 42; // a[i] is now an array so this works.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Set Like

a[3]["fieldName"]="xxxx";

3 Comments

That is wrong. Accessing arrays is done by index position. Same applies to multi dimension arrays. a[indexNumber][indexNumber].
you could explain the answer.That will help others if the answer is correct
you pushes the array as Arr.push({ TimeSheetID: 0, ProjectID: 1, ProjectName: "Test", TaskName: "My Task", TaskType: TaskType, Status: 0, IsNew: true }); and hence your array object contains 10 records, If you want to change value of ProjectName property at 3rd index then you should write as : Arr[3]["ProjectName"]="xxxxxxx";

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.