15

I have a JSON array like this:

_htaItems = [
    {"ID":1,
     "parentColumnSortID":"0",
     "description":"Precondition",
     "columnSortID":"1",
     "itemType":0},
    {"ID":2,
     "parentColumnSortID":"0",
     "description":"Precondition",
     "columnSortID":"1",
    "itemType":0}]

I want to update this by passing the ID, column name and new value to a function:

    function updateJSON(ID, columnName, newValue)
    {
        var i = 0;
        for (i = 0; i < _htaItems.length; i++)
        {
            if (_htaItems[i].ID == ID)
            {
                ?????
            }
        }
    }  

My question is, how do I update the value? I know I can do something like the following:

 _htaItems[x].description = 'New Value'

But in my cause, the column name is being passed as a string.

9
  • 2
    You have a JavaScript array, not JSON. Commented Aug 11, 2011 at 13:56
  • possible duplicate of How to use a variable value for the key of another object? Commented Aug 11, 2011 at 13:58
  • 1
    @jagdipa Felix is right--no JSON here. You have an array built using the array literal syntax which contains objects that were built with the object literal syntax. Commented Aug 11, 2011 at 14:08
  • @Felix - this is a possible duplicate to sundry questions, but I couldn't quickly find one that used square bracket notation more than one level deep. Commented Aug 11, 2011 at 14:08
  • @JAAulde: Does not really make a difference. Commented Aug 11, 2011 at 14:08

4 Answers 4

32

In JavaScript, you can access an object property either with literal notation:

the.answer = 42;

Or with bracketed notation using a string for the property name:

the["answer"] = 42;

Those two statements do exactly the same thing, but in the case of the second one, since what goes in the brackets is a string, it can be any expression that resolves to a string (or can be coerced to one). So all of these do the same thing:

x = "answer";
the[x] = 42;

x = "ans";
y = "wer";
the[x + y] = 42;

function foo() {
    return "answer";
}
the[foo()] = 42;

...which is to set the answer property of the object the to 42.

So if description in your example can't be a literal because it's being passed to you from somewhere else, you can use bracketed notation:

s = "description";
_htaItems[x][s] = 'New Value';
Sign up to request clarification or add additional context in comments.

3 Comments

What na elequent explanation ! I've read thousands of articles and could't get THE answer. Just save my day. Thank you!
@T.J. Crowder: what if the referenced property is a string representing some arbitrary levels deep, eg "a.b[5].c", of an arbitrary object? Is there a smart way to avoid parsing the string into components and accessing each property one at a time?
@OldGeezer - I'm afraid not. :-) Move in this question's answers: stackoverflow.com/questions/6491463/…
1

_htaItems[x][columnName] = 'New Value'; Or did I misunderstand you?

1 Comment

To be fair, it's probably the time it took you to press the "code-style" button ;)
0

Just do _htaItems[i][columnName] = newValue;. It will change the property specified in columnName to newValue.

Comments

0

You need to use square bracket notation, just like you did for array index:

_htaItems[i][columnName] = newValue;

1 Comment

Well that was a embarassingly easy :D

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.