0

I was trying to follow this Getting JSON object values by name but I can't get it.

My data comes from this JSON: The green boxes show the values arrays and objets that I need to use.

http://fotos.subefotos.com/2a862e55595038ab73ab2f0805b83c15o.png

So I need to find the object inside the array "options" which has the id == 233 and then return it's "value", I'm trying to do it with this function:

function getReqVal(val) {
        for (var item in datos_puros[0].campos[0].campos[0].options) {
            if(item.id == val) {
               return item.value;
            }
        }
    }

I'm bringin the val like this:

getReqVal(datos_puros[0].campos[0].campos[0].value[0])
//console.log(datos_puros[0].campos[0].campos[0].value[0]) --> 233, it works

(datos_puros comes from an previous ajax call)

datos_puros = obj.estructura;

but I can't see where's my error. Please help

This is was I'm trying to do: https://jsfiddle.net/pmiranda/jrnfL9hp/1/

Community
  • 1
  • 1
pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • What result do you get now? Do you get a console error? If so, what is the error? – musefan Oct 11 '16 at 15:18
  • You expected the `for..in` loop to behave like a `for..of` loop. But it doesn't. That's one of the reasons you shouldn't use it to iterate arrays. – Oriol Oct 11 '16 at 15:21
  • @musefan it's not entering the "if". I get this for (var item in datos_puros[0].campos[0].campos[0].options) { console.log(item.id); // undefined, undefined, undefined, ... console.log(item); // 1 ,2 ,3 ..., 51 (all the objects) if(item.id == val) { return item.value; } } – pmiranda Oct 11 '16 at 15:30
  • This is what I'm trying to do: https://jsfiddle.net/pmiranda/jrnfL9hp/ – pmiranda Oct 11 '16 at 16:11

0 Answers0