Skip to content
Advertisement

Filter array object by type and compare key

I have two arrays, in one (aux) I get key and value. In the second array of objects (result)

enter image description here

I have the attribute “correlati” and what I’m looking for is that if the key of the array “aux” is equal to the attribute “correlati” then it pushes that result in a new array that looks like the following :

[{
   correlative: "G-22-1-06",
   content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 6d 69 27 50 72 6f 64 ... 277154 more bytes>,
name_Patient: "Jeidi Nicol Matute Banegas"
},
{
  correlative: "G-22-1-07",
   content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 6d 69 27 50 72 6f 64 ... 275969 more bytes>,
name_Patient: "Maria Hercilia Noriega"
}
]

code:

 const newArr: bodyEmailSent[] = [];
              const result = attachment.map( element =>  this.getCantidad.find( y => element.content === y.content ) )
              console.log('result', result)
              
              //const aux = this.namePatient.find( y => result.map( element => element.correlativo_solicitud ) === y )
              console.log('Aux', this.namePatient);
             

Advertisement

Answer

You can loop through result array and check if array_element["correlative"] exists in aux array as key. So,…

// Your 2 arrays
let result = []
let aux = []

let ans = []
result.forEach(ele=>{
    expected_key = res["correlative"];
    // check if above key is present in aux
    if(aux[expected_key]){
        // crate a copy so items in result also doesn't update
        let newItem={...ele};
        // Add new required key, value
        newItem["name_Patient"]=aux[expected_key]
        // add new item to ans array
        ans.push(newItem)
    }
})

Maybe this is what you wanted

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement