Convert this to javascript

84 Views Asked by At

I have some Swift code that needs to be written in javascript. I am not sure how to do this. jsonresult is the json data. Can anyone help?

let results: NSDictionary = jsonresult["results"] as! NSDictionary

let collection1: NSArray = results["collection1"] as! NSArray

for item in collection1 {

Thanks

1

There are 1 best solutions below

2
On BEST ANSWER

let results: NSDictionary = jsonresult["results"] as! NSDictionary

var results = jsonresult["results"];

let collection1: NSArray = results["collection1"] as! NSArray

var collection1 = results["collection1"];

for item in collection1 {

for(var item in collection1){

In JS item will the index. So to access the value from the array you need to access the index of the array collection1[item]

If jsonResult is an object you can simply access collection1 like this

var jsonResult = {
   'results' : 
      { 'collection1': ["a", "b", "c", "d", "e"] }
};

var collection1 = jsonResult.results.collection1;