I am experimenting with deeply nested array filtering with JavaScript. I have created a solution which works but would anyone be able to demonstrate any alternative methods (faster or more efficient) how to accomplish the same task provided with the code below. function carReturn()
{
return myCars.filter(myCars => myCars.colour.includes("Red"));
}
The scenario and the purpose of the code is to filter out the cars, which have color "Red" attached to them.
Below I have attached the full demonstration of the code.
Thanks for any help or recommendations.
<p>Car List.</p>
<p id="show"></p>
<p id="show1"></p>
<script>
const myCars = [
{ name: "BMW",colour: ["White","Red","Black"] },
{ name: "AUDI",colour: ["Yellow","Silver"] },
{ name: "VW",colour: ["Purple","Gold"] },
{ name: "NISSAN",colour: ["White","Black"] },
{ name: "SUZUKI",colour: ["Red"] },
{ name: "Lada",colour: ["Gray","Red"] },
];
function carReturn()
{
return myCars.filter(myCars => myCars.colour.includes("Red"));
}
var jol = carReturn();
document.getElementById("show").innerHTML += jol;
console.log(carReturn('Red'));
</script>
PS the innerHTML does not work but the functionality works within console.
The posts used for reference has been:
JavaScript filtering
Filtering an array with a deeply nested array in JS