-2
const data = [
 {
  groups: [
      {
          "raceEthnicity": {
              "Black or African American": 45,
              "American Indian or Alaska Native": 18,
              "Asian - - East Asian": 27,
              "White - Arabic/North African": 7,
              "White - White/Caucasian/European": 142,
              "Mixed/Other/Unspecified": 3
          }
      },
      {
          "raceEthnicity": {
              "Black or African American": 48,
              "American Indian or Alaska Native": 14,
              "Asian - East Asian": 36,
              "Asian - South East Asian": 3,
              "White - Arabic/North African": 4,
              "White - White/Caucasian/European": 140,
              "Mixed/Other/Unspecified": 2
          }
      }
  ]
 },
 {
  "groups": [
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 11,
              "Black or African American": 47,
              "Asian - South East Asian": 0,
              "White - Arabic/North African": 1,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      },
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 4,
              "Black or African American": 28,
              "Asian - South East Asian": 2,
              "White - Arabic/North African": 0,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      }
  ]
 },
 {
  "groups": [
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 11,
              "Black or African American": 47,
              "Asian - South East Asian": 0,
              "White - Arabic/North African": 1,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      },
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 4,
              "Black or African American": 28,
              "Asian - South East Asian": 2,
              "White": 20,
              "Mixed/Other/Unspecified": 1
          },
      }
  ]
 }
]

Hello guys, I have an object who look like this, and I'm training to write a filter who take an array of values like ["Asian", "White"] and return the objects who have ["Asian", "White"] > 0 . So if the object raceEthnicity have Asian - East Asian, should be pick because Asian - East Asian also contain Asian people.

DenisPaul
  • 3
  • 3
  • 3
    what is your expected result? – Layhout Dec 15 '22 at 08:31
  • If i select "asian", it should result all the objects from array who contain the word asian and have the values bigger then 0. even the one whit (Asina - Shouth East Asian) – DenisPaul Dec 15 '22 at 08:36
  • 2
    Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=javascript+filter+object+using+aray+site%3Astackoverflow.com). If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Dec 15 '22 at 08:39

2 Answers2

0

const data = [
    {
        groups: [
            {
                "raceEthnicity": {
                    "Black or African American": 45,
                    "American Indian or Alaska Native": 18,
                    "Asian - - East Asian": 0,
                    "White - Arabic/North African": 7,
                    "White - White/Caucasian/European": 142,
                    "Mixed/Other/Unspecified": 3
                }
            },
            {
                "raceEthnicity": {
                    "Black or African American": 45,
                    "American Indian or Alaska Native": 18,
                    "Asian - - East Asian": 27,
                    "White - Arabic/North African": 7,
                    "White - White/Caucasian/European": 142,
                    "Mixed/Other/Unspecified": 3
                }
            },
        ]
    }
]

//Store the items whichs accomplish the conditions filterArrayValues
let valid = [];
//Always must be Array.
let filterArrayValues = ["Asian", "White"];
filterArrayValues.forEach((filter, index) => {
    //Extract the groups of data
    for (let { groups } of data) {
        //Extract the raceEthnicity of groups
        for (let { raceEthnicity } of groups) {
            //Filter condition getting the key String
            let filtered = Object.entries(raceEthnicity).filter(entry => {
                /*
                    entry[0] is the string to check if includes "Asia" or whatever.
                    +entry[1] is the numeric which is evaluated with the condition > 0.
                    join is for get a string of filtered values.
                */
                return entry[0].includes(filter) && +entry[1] > 0;
            }).join();
            // then got filtered data
            if (filtered != "") {
                //do the inverse to make up the object again
                filtered.split(",").forEach((c, i, a) => {
                    if (i % 2 == 0) {
                        let object = {};
                        object[c] = +a[++i];
                        valid.push({
                            index,
                            filtered: object
                        })
                    }
                });
            }
        }
    }
});

console.dir(valid)
0

const data = [
    {
        groups: [
            {
                raceEthnicity: {
                    "Black or African American": 45,
                    "American Indian or Alaska Native": 18,
                    "Asian - - East Asian": 27,
                    "White - Arabic/North African": 7,
                    "White - White/Caucasian/European": 142,
                    "Mixed/Other/Unspecified": 3
                }
            },
            {
                raceEthnicity: {
                    "Black or African American": 48,
                    "American Indian or Alaska Native": 14,
                    "Asian - East Asian": 36,
                    "Asian - South East Asian": 3,
                    "White - Arabic/North African": 4,
                    "White - White/Caucasian/European": 140,
                    "Mixed/Other/Unspecified": 2
                }
            }
        ]
    },
    {
        groups: [
            {
                raceEthnicity: {
                    "American Indian or Alaska Native": 11,
                    "Black or African American": 47,
                    "Asian - South East Asian": 0,
                    "White - Arabic/North African": 1,
                    "White - White/Caucasian/European": 9,
                    "Mixed/Other/Unspecified": 1
                },
            },
            {
                raceEthnicity: {
                    "American Indian or Alaska Native": 4,
                    "Black or African American": 28,
                    "Asian - South East Asian": 2,
                    "White - Arabic/North African": 0,
                    "White - White/Caucasian/European": 9,
                    "Mixed/Other/Unspecified": 1
                },
            }
        ]
    },
    {
        groups: [
            {
                raceEthnicity: {
                    "American Indian or Alaska Native": 11,
                    "Black or African American": 47,
                    "Asian - South East Asian": 0,
                    "White - Arabic/North African": 1,
                    "White - White/Caucasian/European": 9,
                    "Mixed/Other/Unspecified": 1
                },
            },
            {
                raceEthnicity: {
                    "American Indian or Alaska Native": 4,
                    "Black or African American": 28,
                    "Asian - South East Asian": 2,
                    "White": 20,
                    "Mixed/Other/Unspecified": 1
                },
            }
        ]
    }
]

const filterArr = keyWord => {
    if (!keyWord) {
        console.log("please enter a key word");
        return;
    }
    return data.reduce((p, { groups }) => {
        const tmpArr = groups.reduce((p, { raceEthnicity }) => {
            const tmpObj = { raceEthnicity: {} };
            for (const k in raceEthnicity) if (k.includes(keyWord) && raceEthnicity[k] > 0) {
                tmpObj.raceEthnicity[k] = raceEthnicity[k];
            }
            return Object.keys(tmpObj.raceEthnicity).length > 0 ? p.concat(tmpObj) : p;
        }, []);
        return tmpArr.length > 0 ? p.concat({ groups: tmpArr }) : p;
    }, [])
}

console.log(filterArr("Asian"));
Layhout
  • 1,445
  • 1
  • 3
  • 16