I would like to know how can I get a value from an array of array using map javascript function.
My response from a query :
[
{
customer_id: '5d0757aa4b6620003335aff2',
cars: [
{
type: 'break', // unique
serial_number: '0000X523654FE'
// ... other data, not useful here
},
{
type: 'sport',
serial_number: '485605FEL45E'
// ...
}
]
}
]
I want to get serial_number string, so here what I did with map javascript function :
const serialNumber = response.items.map(element => {
return element.cars.map(car => {
if (car.type == "sport") {
return car.serial_number;
}
});
});
And I get :
[ [ '485605FEL45E' ] ]
I would like to get : '485605FEL45E'
How can remove the double array ? I just want the value.
Thank you very much !
EDIT : I didn't specify something important, type is unique, I can't get two sport cars for the same customer.
serialNumber[0][0]????.flat()or.flatMap()if you want to remove the inner array?.findoncarsarray notmap.'485605FEL45E', this implies your only want 1 single results.. Is this correct, and if so is it just the first it finds.? Multiple results would look like ->['abc', '123'],...