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);