0

I have the following JSON object

{
    "class": "go.GraphLinksModel",
        "nodeDataArray": [{
        "key": "CTR:2",
            "type": "Controller",
            "devicename": "ACU-1K",
            "deviceid": "2",
            "imageUrl": "http://localhost:52233/Images/ComputerSpeaker_32.png",
            "loc": "280 112"
    }, {
        "key": "CTR:1",
            "type": "Controller",
            "devicename": "C302-1",
            "deviceid": "1",
            "imageUrl": "http://localhost:52233/Images/ComputerSpeaker_32.png",
            "loc": "477 92"
    }, {
        "key": "RDR:25",
            "type": "Reader",
            "devicename": "1K-1-RDR01",
            "deviceid": "25",
            "imageUrl": "http://localhost:52233/Images/Plugin_32.png",
            "loc": "592 41"
    }],
        "linkDataArray": []
}

I would like to loop through nodeDataArray, and change the value of imageUrl for each item within nodeDataArray

I tried this, but it doesn't work

$.each(jsonCoordinates, function(key, value) {
    var nodeData = value.nodeDataArray;
    $.each(nodeData, function(k, v) {
        v.imageUrl = "default";
    });

});

How should it be?

jsonCoordinates contains the whole chunk of JSON above

2 Answers 2

1

You don't need nested $.each methods, iterate through the nodeDataArray directly.

$.each(jsonCoordinates.nodeDataArray, function(index, value) {
    value.imageUrl = "default";
});

http://jsfiddle.net/XSzYM/

A suggestion: always use console.log() for debugging your code, specially for iterating through nested objects and arrays.

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

Comments

1

Since your jsonCoordinates is not an array, when you do the each it doesn't execute. I think your code would work if you wrap it around [];

http://jsfiddle.net/Xu9gH/1/

var Data = {"class":"go.GraphLinksModel","nodeDataArray":[{"key":"CTR:2","type":"Controller","devicename":"ACU-1K","deviceid":"2","imageUrl":"http://localhost:52233/Images/ComputerSpeaker_32.png","loc":"280 112"},
{"key":"CTR:1","type":"Controller","devicename":"C302-1","deviceid":"1","imageUrl":"http://localhost:52233/Images/ComputerSpeaker_32.png","loc":"477 92"},
{"key":"RDR:25","type":"Reader","devicename":"1K-1-RDR01","deviceid":"25","imageUrl":"http://localhost:52233/Images/Plugin_32.png","loc":"592 41"}],
"linkDataArray":[]};

$.each(Data.nodeDataArray, function(k, v) {
 v.imageUrl = "default";
});

console.log(Data);

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.