4

I have json array like

var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'

How to parse the the data and iterate through it using prototype.js?

2

1 Answer 1

1

There is a function called evalJSON()

var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]';
data = data.evalJSON();
//for more security:
data = data.evalJSON(true); //sanitizes data before processing it

Then just use for to iterate through the array:

for (var i=0;i<data.length;i++)
{ 
    //do whatever you like with data[i];
}

Or use the prototypeJS .each() function:

data.each(function(el){
    //do whatever you like with el
});
Sign up to request clarification or add additional context in comments.

1 Comment

actually you can use the each() method to iterate through the data api.prototypejs.org/language/Enumerable/prototype/each

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.