1

Tengo estos dos array, el primero esta conformado por claves foraneas que vienen de la BD y el segundo por el detalle de una de esas claves foraneas. Como puedo hacer para recorrer la posicion 0 de mi primer array y buscar en el segundo el valor de esa posicion y devolver el detDeterminante:

(6) ["1", "1", "si", "1", "37", "238,835,836"]
    0:"1"
    1:"1"
    2:"si"
    3:"1"
    4:"37"
    5:"238,835,836"
    length:6

(4) [{…}, {…}, {…}, {…}]
    0:{0: "1", 1: "DATO 1", idDeterminante: "1", detDeterminante: "DATO 1"}
    1:{0: "2", 1: "DATO 2", idDeterminante: "2", detDeterminante: "DATO 2"}
    2:{0: "3", 1: "DATO 3", idDeterminante: "3", detDeterminante: "DATO 3"}
    3:{0: "4", 1: "DATO 4", idDeterminante: "4", detDeterminante: "DATO 4"}
    length:4

Ej: introducir la descripción de la imagen aquí

En mi primer array tengo el valor 1 en la posicion 0. Quiero buscar ese valor en las distintas posiciones del array dos y al encontrarlo devolver el detDeterminante que le corresponda

2 respuestas 2

1

Puedes buscar comparando el indice 0 del primer array con la propiedad idDeterminante del segundo. Algo asi:

var lista1= ["1", "1", "si", "1", "37", "238,835,836"];
var lista2= [
    {0: "1", 1: "DATO 1", idDeterminante: "1", detDeterminante: "DATO 1"},
    {0: "2", 1: "DATO 2", idDeterminante: "2", detDeterminante: "DATO 2"},
    {0: "3", 1: "DATO 3", idDeterminante: "3", detDeterminante: "DATO 3"},
    {0: "4", 1: "DATO 4", idDeterminante: "4", detDeterminante: "DATO 4"}
];

function buscar_en(posicion){
    var obtenido= lista2.filter( function( item){  
        return item.idDeterminante == posicion;
    })[0];
    console.log(  obtenido.detDeterminante);
}

buscar_en(lista1[0]);

1
  • Muchas gracias, me sirvio
    – Juan
    el 18 sep. 2018 a las 19:57
0
var lista1= ["1", "1", "si", "1", "37", "238,835,836"];
var lista2= [
    {0: "1", 1: "DATO 1", idDeterminante: "1", detDeterminante: "DATO 1"},
    {0: "2", 1: "DATO 2", idDeterminante: "2", detDeterminante: "DATO 2"},
    {0: "3", 1: "DATO 3", idDeterminante: "3", detDeterminante: "DATO 3"},
    {0: "4", 1: "DATO 4", idDeterminante: "4", detDeterminante: "DATO 4"}
];

function buscar_en(posicion){
    var obtenido= lista2.filter( function( item){  
        return item.idDeterminante == lista1[ posicion]; //  posicion 0
    })[0];
    console.log(  obtenido.detDeterminante);
}

buscar_en(0);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.