JavaScript JavaScript Deeply Nested Array filtering javascript deeply nested objects,js deeply nested objects,javascript flatten



I have created a somehow functional "deeply" nested array filtering functionality. This functionality displays car, which has a color "RED" assigned to them. However the cars, which "pass" the filter is the car, which only has 1 color assigned.
How can ensure that arrays within arrays are being checked by the criteria? Would I require creating a loop within loop? The desired outcome would be that if criteria is set to "RED" it will also display the BMW and SUZUKI as these cars also have this color.

Any help, suggestions or further links to other posts to achieve the desired outcome would be truly appreciated as I was not able to find anything very useful on my own. The post, which has been most useful has been this one - Filtering an array with a deeply nested array in JS

Below I have attached the code snippet of my current code.

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"] }, 
];

for (x in myCars) {
  var keys = Object.keys(myCars[x])

  var carSpec = myCars.filter(function(fltr) {
    return (fltr.colour == "Red");

  });
  document.getElementById("show1").innerHTML += carSpec[x].name + " " + "has these colours - " + carSpec[x].colour + "<hr />";
}
<p>Car List.</p>

<p id="show"></p>
<p id="show1"></p>
Answer:1

You need to filter by an includes test over the colour array:

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"] }, 
];
const show1 = document.getElementById("show1");
myCars
  .filter(({ colour }) => colour.includes('Red'))
  .forEach(({ name, colour }) => {
    show1.innerHTML += name + " " + "has these colours - " + colour + "<hr />";
  });
<p>Car List.</p>

<p id="show"></p>
<p id="show1"></p>
Answer:2

I am trying to pass an array of objects like "make":"Ford", "trim":"escape", "year":2014 into a JSON object car at following exaample var player = { "name":"John", "age":30, "car": [{"...

I am trying to pass an array of objects like "make":"Ford", "trim":"escape", "year":2014 into a JSON object car at following exaample var player = { "name":"John", "age":30, "car": [{"...

  1. pass array json
  2. pass array json ajax
  3. pass json array to spring mvc controller
  4. pass json array to mvc controller
  5. pass json array in postman
  6. pass json array to webmethod c#
  7. pass json array to web api c#
  8. pass json array to javascript function
  9. pass json array in ajax call
  10. pass json array to php
  11. pass json array to another activity android
  12. pass json array as parameter
  13. pass json array in url
  14. pass json array in intent
  15. pass json array from php to javascript
  16. pass json array to web api
  17. pass json array to mvc controller c#
  18. pass json array to mvc controller using ajax
  19. pass json array to controller
  20. pass json array to mvc controller java

this has been bugging me for ages but I can't figure out how to programmatically change an ion-toggle value. My toggle starts as on. When someone slides it off, I popup a confirmation. If they cancel ...

this has been bugging me for ages but I can't figure out how to programmatically change an ion-toggle value. My toggle starts as on. When someone slides it off, I popup a confirmation. If they cancel ...

Good evening everyone, I have an array which contains 24 characters "hours": [ "1", "1", "1", "1", "0", "0", "0", "0", "0", ...

Good evening everyone, I have an array which contains 24 characters "hours": [ "1", "1", "1", "1", "0", "0", "0", "0", "0", ...

  1. replace specific value in pandas dataframe
  2. replace specific value in dataframe r
  3. replace specific value in column pandas
  4. replace specific value in list python
  5. replace specific value in r
  6. replace specific value in array javascript
  7. replace specific value in array php
  8. replace specific value in dataframe python
  9. replace specific value pandas
  10. replace specific value in column r
  11. replace specific value in dataframe
  12. replace specific value with na in r
  13. replace specific value in csv python
  14. replace specific value in string python
  15. replace specific value in excel
  16. replace specific value stata
  17. replace specific value in sql
  18. replace specific value in spotfire
  19. replace specific value in list java
  20. replace specific value in array

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...