1

I have the following JSON. I have a parameter to give and I need to return all the number.

If I give "BUS", I need to return1,38,44,58

Example of JSON:

{  
   _id:"23456789",
   result:[  
      [  
         "Car",
         [  
            [  
               2,
               3,
               4,
               6
            ],
            [  
               3,
               4,
               444,
               123
            ],
            [  
               43,
               34,
               91446,
               344473
            ]
         ]
      ],
      [  
         "Bus",
         [  
            [  
               1,
               38,
               4458,
               0981
            ]
         ]
      ],
      [  
         "Moto",
         [  
            [  
               5,
               43,
               41440,
               804444
            ]
         ]
      ]
   ]
}

This is my code:

    var coordinates = [];
    console.log("tag :"+tag); // tag is the parameter "Bus", "Car" or "Moto"
    $http.get('http://myserver:1234/bbox/'+id)
                    .success(function (response) {
                var point = {};
                // Don't know how to catch a specific word (i.e Car or BUS or Moto)
                for (var i in response){
                    var pointName =  response.result[i][0];
                    coordinates.push(response.result[i][1]);
                    points[pointName] = coordinates;
                }

    })
    .error(function (response) {
        console.log(response);
     });

tag is already set with only one parameter. Just need to return the coordinate for one given.

Thanks for your help!

1 Answer 1

2

This is pretty strange structure for JSON response, but you can still extract necessary data. For example using Array.prototype.filter method:

var coordinates = response.result.filter(function(el) {
    return el[0] === tag;
})[0][1][0];

For tag equal to "Bus" above code will give you [1, 38, 4458, 981] array.

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

4 Comments

Thanks for your reply. Could I do something like var coordinates = response.result.filter(function(el) { var res = el[0] === tag; console.log("res "+res); })[0][1][0]; ????
Not sure I follow. Filter must return boolean value.
OOOps my mistake, it works! About the tag "Car", I will also get all the points?
Looks like you actually need filter()[0][1] to get all points array (array of array of all points).

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.