Find a property of an object for its value, and in turn collect others from it. Javascript

1

I have a query. Starting from the function and the object that you see, I have an application that will launch a state (a string of characters) that is in a variable. I need to find the value of a property within an object and if it exists, it will pick up two more values of the same level.

Therefore, if the state I send to you is "state2", you can send the text of your level ("Test text2") to another function stored in one variable and the type ("negative") in another variable. The function to find a property for the state I have, but what I need is this, that if I find the "state2", I capture ONLY those of that level, and that is what I do not get ... how I have to focus the development?.

var matrizObjeto = {
        0: {
            "Texto": "Prueba texto1",
            "Estado": "estado1",
            "Tipo": "positivo"
        },
        1: {
            "Texto": "Prueba texto2",
            "Estado": "estado2",
            "Tipo": "negativo"
        },
        2: {
            "Texto": "Prueba texto3",
            "Estado": "estado3",
            "Tipo": "negativo"
        }
    };

    var estado = "estado2";
    var parcialEstado;
    var parcialTexto;
    var parcialTipo


    function searchObj(obj, query) {
        for (var key in obj) {
            var value = obj[key];
            if (typeof value === 'object') {
                searchObj(value, query);
            }
            if (value === query) {
                (estado === value) ? parcialClave = value : alert("no");
            }
        }
    }

    searchObj(matrizObjeto, estado);
    
asked by djohny 17.11.2016 в 19:07
source

4 answers

1

According to what I understand you want to do this:

function searchObj(obj, query) {
    for (k in obj) {
        if (obj[k]['Estado'] === query){
            return [ obj[k]['Texto'], obj[k]['Tipo']];
        }
    }
}
    
answered by 17.11.2016 / 19:58
source
1

Let's see, starting from the fact that you want to find and capture the information that is in that object by the state of each "sub-object" (or level as we call it), you can do it in the following way:

var obj = {
  0: {
    text: "Some dummy text in obj[0]",
    status: "status_0",
    type: "negative"
  },
  1: {
    text: "Some dummy text in obj[1]",
    status: "status_1",
    type: "negative"
  },
  2: {
    text: "Some dummy text in obj[2]",
    status: "status_2",
    type: "negative"
  },
};

function findInObj(obj, status) {
  for (var key in obj) {
    var val = obj[key].status;
    if (status == val) {
      alert(JSON.stringify(obj[key], null, 4)); // Aquí obtienes todo el objeto de tu nivel
      alert(obj[key].text);                     // <- Aquí obtienes el text de tu nivel
      alert(obj[key].status);                   // <- Aquí obtienes el status de tu nivel
      alert(obj[key].type);                     // <- Aquí obtienes el type de tu nivel
    }
  }
}

findInObj(obj, "status_0");
    
answered by 17.11.2016 в 19:29
1

You can do the following, in your function you make a each of your arrangement

var matrizObjeto = {
    0: {
        "Texto": "Prueba texto1",
        "Estado": "estado1",
        "Tipo": "positivo"
    },
    1: {
        "Texto": "Prueba texto2",
        "Estado": "estado2",
        "Tipo": "negativo"
    },
    2: {
        "Texto": "Prueba texto3",
        "Estado": "estado3",
        "Tipo": "negativo"
    }
};

Jquery:

var parcialEstado = "";
var parcialTexto = "";
var parcialTipo = "";
function searchObj(estado) {
    $.each(matrizObjeto, function (index, value) {
      if(value.Estado == estado){
         parcialEstado = value.Estado;
         parcialTexto = value.Texto;
         parcialTipo = value.Tipo;
      }
    });
}
var estado = "estado2";
searchObj(estado);
console.log(parcialEstado);
console.log(parcialTexto);
console.log(parcialTipo);

Javascript

var matrizObjeto = {
0: {
    "Texto": "Prueba texto1",
    "Estado": "estado1",
    "Tipo": "positivo"
},
1: {
    "Texto": "Prueba texto2",
    "Estado": "estado2",
    "Tipo": "negativo"
},
2: {
    "Texto": "Prueba texto3",
    "Estado": "estado3",
    "Tipo": "negativo"
}
};
var parcialEstado = "";
var parcialTexto = "";
var parcialTipo = "";

function searchObj(estado) {
for (var i in matrizObjeto) {
    if (matrizObjeto[i].Estado == estado) {
        parcialEstado = matrizObjeto[i].Estado;
        parcialTexto = matrizObjeto[i].Texto;
        parcialTipo = matrizObjeto[i].Tipo;
    }
}
}
var estado = "estado2";
searchObj(estado);
alert(parcialEstado);
alert(parcialTexto);
alert(parcialTipo);
    
answered by 17.11.2016 в 19:33
0

The truth is that you have all hit the target :) Since the program that contains the object / objects to be treated is visually easier to recognize the Neyer option, it is the one that I will use. Thanks to everyone!

    
answered by 18.11.2016 в 08:27