使用另一个数组中的值过滤数组

时间:2017-09-26 17:16:07

标签: javascript arrays

我有一个数组,我希望根据另一个数组中的键进行迭代和过滤。如果代码与第二个数组匹配,我需要遍历第一个数组并删除该项。我知道我应该是使用数组过滤器,但我无法了解如何将第二个数组作为过滤器传递。



var arr =[
  [
    {
      "name": "SEASONAL_LYQ1",
      "code": "SEASONAL_LYQ1",
      "parent": "SEASONAL_POP",
      "value": 0,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ2",
      "code": "SEASONAL_LYQ2",
      "parent": "SEASONAL_POP",
      "value": 19,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ3",
      "code": "SEASONAL_LYQ3",
      "parent": "SEASONAL_POP",
      "value": 40,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ4",
      "code": "SEASONAL_LYQ4",
      "parent": "SEASONAL_POP",
      "value": 22,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ1",
      "code": "SEASONAL_CYQ1",
      "parent": "SEASONAL_POP",
      "value": 0,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ2",
      "code": "SEASONAL_CYQ2",
      "parent": "SEASONAL_POP",
      "value": 20,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ3",
      "code": "SEASONAL_CYQ3",
      "parent": "SEASONAL_POP",
      "value": 40,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ4",
      "code": "SEASONAL_CYQ4",
      "parent": "SEASONAL_POP",
      "value": 24,
      "children": []
    }
  ],
  [
    {
      "name": "Total Educated",
      "code": "Q035001",
      "parent": "EDU_ATTAINMENT",
      "value": "9891",
      "children": []
    },
    {
      "name": "Grade  Less than 9",
      "code": "Q035003",
      "parent": "EDU_ATTAINMENT",
      "value": "514",
      "children": []
    },
    {
      "name": "Grade 9 to 12",
      "code": "Q035007",
      "parent": "EDU_ATTAINMENT",
      "value": "444",
      "children": []
    },
    {
      "name": "High School",
      "code": "Q035011",
      "parent": "EDU_ATTAINMENT",
      "value": "1269",
      "children": []
    },
    {
      "name": "Some College",
      "code": "Q035012",
      "parent": "EDU_ATTAINMENT",
      "value": "2001",
      "children": []
    },
    {
      "name": "College Degree -  Associate's",
      "code": "Q035014",
      "parent": "EDU_ATTAINMENT",
      "value": "400",
      "children": []
    },
    {
      "name": "College Degree -  Bachelor's",
      "code": "Q035015",
      "parent": "EDU_ATTAINMENT",
      "value": "3018",
      "children": []
    },
    {
      "name": "College Degree -  Master's",
      "code": "Q035016",
      "parent": "EDU_ATTAINMENT",
      "value": "1706",
      "children": []
    },
    {
      "name": "College - Professional",
      "code": "Q035017",
      "parent": "EDU_ATTAINMENT",
      "value": "315",
      "children": []
    },
    {
      "name": "College Degree - Doctorate",
      "code": "Q035018",
      "parent": "EDU_ATTAINMENT",
      "value": "224",
      "children": []
    },
    {
      "name": "Enrollments (Total Population)",
      "code": "EDU_Enrollments",
      "parent": "EDU_ATTAINMENT",
      "children": [
        {
          "name": "Nursery school/Preschool",
          "code": "Q036003",
          "parent": "EDU_Enrollments",
          "value": "330",
          "children": []
        },
        {
          "name": "Kindergarten/Elementary school",
          "code": "Q036006",
          "parent": "EDU_Enrollments",
          "value": "1158",
          "children": []
        },
        {
          "name": "High School",
          "code": "Q036015",
          "parent": "EDU_Enrollments",
          "value": "636",
          "children": []
        },
        {
          "name": "College/Graduate /Professional school",
          "code": "Q036018",
          "parent": "EDU_Enrollments",
          "value": "1715",
          "children": []
        },
        {
          "name": "Not Enrolled",
          "code": "Q036024",
          "parent": "EDU_Enrollments",
          "value": "10324",
          "children": []
        }
      ]
    },
    {
      "name": "Percents",
      "code": "PCT_EDU_ATTAINMENT",
      "parent": "EDU_ATTAINMENT",
      "children": [
        {
          "name": "% Grade  Less than 9",
          "code": "XQ035003",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "5.197",
          "children": []
        },
        {
          "name": "% Grade 9 to 12",
          "code": "XQ035007",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "4.4937",
          "children": []
        },
        {
          "name": "% High school",
          "code": "XQ035011",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "12.8274",
          "children": []
        },
        {
          "name": "% Some college",
          "code": "XQ035012",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "20.2347",
          "children": []
        },
        {
          "name": "% College - Associate",
          "code": "XQ035014",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "4.0479",
          "children": []
        },
        {
          "name": "% College - Bachelors",
          "code": "XQ035015",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "30.509",
          "children": []
        },
        {
          "name": "% College - Masters",
          "code": "XQ035016",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "17.2467",
          "children": []
        },
        {
          "name": "% College - Professional",
          "code": "XQ035017",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "3.1824",
          "children": []
        },
        {
          "name": "% College - Doctorate",
          "code": "XQ035018",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "2.2612",
          "children": []
        }
      ]
    }
  ],
  [
    {
      "name": "1 Person HHs",
      "code": "Q014010",
      "parent": "HH_by_size",
      "value": "2395",
      "children": []
    },
    {
      "name": "2 Person HHs",
      "code": "Q014011",
      "parent": "HH_by_size",
      "value": "2140",
      "children": []
    },
    {
      "name": "3 Person HHs",
      "code": "Q014012",
      "parent": "HH_by_size",
      "value": "878",
      "children": []
    },
    {
      "name": "4 Person HHs",
      "code": "Q014013",
      "parent": "HH_by_size",
      "value": "701",
      "children": []
    },
    {
      "name": "5 Person HHs",
      "code": "Q014014",
      "parent": "HH_by_size",
      "value": "247",
      "children": []
    },
    {
      "name": "6 Person HHs",
      "code": "Q014015",
      "parent": "HH_by_size",
      "value": "68",
      "children": []
    },
    {
      "name": "7+ Person HHs",
      "code": "Q014016",
      "parent": "HH_by_size",
      "value": "54",
      "children": []
    },
    {
      "name": "Persons Per HH",
      "code": "Q017001",
      "parent": "HH_by_size",
      "value": "2",
      "children": []
    }
  ],
  [
    {
      "name": "Occ Managerial Executive",
      "code": "Q050004",
      "parent": "EMP_16P_WHITE_COL",
      "value": "1748",
      "children": []
    },
    {
      "name": "Occ Prof Specialty",
      "code": "Q050010",
      "parent": "EMP_16P_WHITE_COL",
      "value": "2372",
      "children": []
    },
    {
      "name": "Occ Healthcare Support",
      "code": "Q050024",
      "parent": "EMP_16P_WHITE_COL",
      "value": "91",
      "children": []
    },
    {
      "name": "Occ Sales",
      "code": "Q050032",
      "parent": "EMP_16P_WHITE_COL",
      "value": "1323",
      "children": []
    },
    {
      "name": "Occ Office Admin",
      "code": "Q050033",
      "parent": "EMP_16P_WHITE_COL",
      "value": "623",
      "children": []
    }
  ],
  [
    {
      "name": "Occ Protective",
      "code": "Q050025",
      "parent": "EMP_16P_BLUE_COL",
      "value": 75,
      "children": []
    },
    {
      "name": "Occ Food Preparation Serving",
      "code": "Q050028",
      "parent": "EMP_16P_BLUE_COL",
      "value": 394,
      "children": []
    },
    {
      "name": "Occ Bldg Maintenance & Cleaning",
      "code": "Q050029",
      "parent": "EMP_16P_BLUE_COL",
      "value": 183,
      "children": []
    },
    {
      "name": "Occ Personal Care",
      "code": "Q050030",
      "parent": "EMP_16P_BLUE_COL",
      "value": 421,
      "children": []
    },
    {
      "name": "Occ Farming, Fishing & Forestry",
      "code": "Q050034",
      "parent": "EMP_16P_BLUE_COL",
      "value": 0,
      "children": []
    },
    {
      "name": "Occ Construction",
      "code": "Q050035",
      "parent": "EMP_16P_BLUE_COL",
      "value": 330,
      "children": []
    },
    {
      "name": "Occ Production Transportation",
      "code": "Q050041",
      "parent": "EMP_16P_BLUE_COL",
      "value": 404,
      "children": []
    }
  ],
  [
    {
      "name": "No Vehicle",
      "code": "Q029003",
      "parent": "VEHICLES_PER_HH",
      "value": "531",
      "children": []
    },
    {
      "name": "1 Vehicle",
      "code": "Q029004",
      "parent": "VEHICLES_PER_HH",
      "value": "2808",
      "children": []
    },
    {
      "name": "2 Vehicle",
      "code": "Q029005",
      "parent": "VEHICLES_PER_HH",
      "value": "2404",
      "children": []
    },
    {
      "name": "3 Vehicle",
      "code": "Q029006",
      "parent": "VEHICLES_PER_HH",
      "value": "555",
      "children": []
    },
    {
      "name": "4 Vehicle",
      "code": "Q029007",
      "parent": "VEHICLES_PER_HH",
      "value": "131",
      "children": []
    },
    {
      "name": "5 Vehicle",
      "code": "Q029008",
      "parent": "VEHICLES_PER_HH",
      "value": "53",
      "children": []
    },
    {
      "name": "Aggregate Number of Vehicle",
      "code": "Q029009",
      "parent": "VEHICLES_PER_HH",
      "value": "10078",
      "children": []
    }
  ]
];

var el = ["Q029009","Q017001","Q035001"];

arr = arr.filter(e => e !== el);
console.log(arr)




y。

1 个答案:

答案 0 :(得分:1)

你有一个数组数组,所以外部数组为.map(),每个内部数组返回一个.filter(),以确保不包含当前对象的.codeel数组中。

var el = ["Q029009","Q017001","Q035001"];
arr = arr.map(a => a.filter(o => !el.includes(o.code)));

演示:



var arr =[
  [
    {
      "name": "SEASONAL_LYQ1",
      "code": "SEASONAL_LYQ1",
      "parent": "SEASONAL_POP",
      "value": 0,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ2",
      "code": "SEASONAL_LYQ2",
      "parent": "SEASONAL_POP",
      "value": 19,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ3",
      "code": "SEASONAL_LYQ3",
      "parent": "SEASONAL_POP",
      "value": 40,
      "children": []
    },
    {
      "name": "SEASONAL_LYQ4",
      "code": "SEASONAL_LYQ4",
      "parent": "SEASONAL_POP",
      "value": 22,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ1",
      "code": "SEASONAL_CYQ1",
      "parent": "SEASONAL_POP",
      "value": 0,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ2",
      "code": "SEASONAL_CYQ2",
      "parent": "SEASONAL_POP",
      "value": 20,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ3",
      "code": "SEASONAL_CYQ3",
      "parent": "SEASONAL_POP",
      "value": 40,
      "children": []
    },
    {
      "name": "SEASONAL_CYQ4",
      "code": "SEASONAL_CYQ4",
      "parent": "SEASONAL_POP",
      "value": 24,
      "children": []
    }
  ],
  [
    {
      "name": "Total Educated",
      "code": "Q035001",
      "parent": "EDU_ATTAINMENT",
      "value": "9891",
      "children": []
    },
    {
      "name": "Grade  Less than 9",
      "code": "Q035003",
      "parent": "EDU_ATTAINMENT",
      "value": "514",
      "children": []
    },
    {
      "name": "Grade 9 to 12",
      "code": "Q035007",
      "parent": "EDU_ATTAINMENT",
      "value": "444",
      "children": []
    },
    {
      "name": "High School",
      "code": "Q035011",
      "parent": "EDU_ATTAINMENT",
      "value": "1269",
      "children": []
    },
    {
      "name": "Some College",
      "code": "Q035012",
      "parent": "EDU_ATTAINMENT",
      "value": "2001",
      "children": []
    },
    {
      "name": "College Degree -  Associate's",
      "code": "Q035014",
      "parent": "EDU_ATTAINMENT",
      "value": "400",
      "children": []
    },
    {
      "name": "College Degree -  Bachelor's",
      "code": "Q035015",
      "parent": "EDU_ATTAINMENT",
      "value": "3018",
      "children": []
    },
    {
      "name": "College Degree -  Master's",
      "code": "Q035016",
      "parent": "EDU_ATTAINMENT",
      "value": "1706",
      "children": []
    },
    {
      "name": "College - Professional",
      "code": "Q035017",
      "parent": "EDU_ATTAINMENT",
      "value": "315",
      "children": []
    },
    {
      "name": "College Degree - Doctorate",
      "code": "Q035018",
      "parent": "EDU_ATTAINMENT",
      "value": "224",
      "children": []
    },
    {
      "name": "Enrollments (Total Population)",
      "code": "EDU_Enrollments",
      "parent": "EDU_ATTAINMENT",
      "children": [
        {
          "name": "Nursery school/Preschool",
          "code": "Q036003",
          "parent": "EDU_Enrollments",
          "value": "330",
          "children": []
        },
        {
          "name": "Kindergarten/Elementary school",
          "code": "Q036006",
          "parent": "EDU_Enrollments",
          "value": "1158",
          "children": []
        },
        {
          "name": "High School",
          "code": "Q036015",
          "parent": "EDU_Enrollments",
          "value": "636",
          "children": []
        },
        {
          "name": "College/Graduate /Professional school",
          "code": "Q036018",
          "parent": "EDU_Enrollments",
          "value": "1715",
          "children": []
        },
        {
          "name": "Not Enrolled",
          "code": "Q036024",
          "parent": "EDU_Enrollments",
          "value": "10324",
          "children": []
        }
      ]
    },
    {
      "name": "Percents",
      "code": "PCT_EDU_ATTAINMENT",
      "parent": "EDU_ATTAINMENT",
      "children": [
        {
          "name": "% Grade  Less than 9",
          "code": "XQ035003",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "5.197",
          "children": []
        },
        {
          "name": "% Grade 9 to 12",
          "code": "XQ035007",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "4.4937",
          "children": []
        },
        {
          "name": "% High school",
          "code": "XQ035011",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "12.8274",
          "children": []
        },
        {
          "name": "% Some college",
          "code": "XQ035012",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "20.2347",
          "children": []
        },
        {
          "name": "% College - Associate",
          "code": "XQ035014",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "4.0479",
          "children": []
        },
        {
          "name": "% College - Bachelors",
          "code": "XQ035015",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "30.509",
          "children": []
        },
        {
          "name": "% College - Masters",
          "code": "XQ035016",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "17.2467",
          "children": []
        },
        {
          "name": "% College - Professional",
          "code": "XQ035017",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "3.1824",
          "children": []
        },
        {
          "name": "% College - Doctorate",
          "code": "XQ035018",
          "parent": "PCT_EDU_ATTAINMENT",
          "value": "2.2612",
          "children": []
        }
      ]
    }
  ],
  [
    {
      "name": "1 Person HHs",
      "code": "Q014010",
      "parent": "HH_by_size",
      "value": "2395",
      "children": []
    },
    {
      "name": "2 Person HHs",
      "code": "Q014011",
      "parent": "HH_by_size",
      "value": "2140",
      "children": []
    },
    {
      "name": "3 Person HHs",
      "code": "Q014012",
      "parent": "HH_by_size",
      "value": "878",
      "children": []
    },
    {
      "name": "4 Person HHs",
      "code": "Q014013",
      "parent": "HH_by_size",
      "value": "701",
      "children": []
    },
    {
      "name": "5 Person HHs",
      "code": "Q014014",
      "parent": "HH_by_size",
      "value": "247",
      "children": []
    },
    {
      "name": "6 Person HHs",
      "code": "Q014015",
      "parent": "HH_by_size",
      "value": "68",
      "children": []
    },
    {
      "name": "7+ Person HHs",
      "code": "Q014016",
      "parent": "HH_by_size",
      "value": "54",
      "children": []
    },
    {
      "name": "Persons Per HH",
      "code": "Q017001",
      "parent": "HH_by_size",
      "value": "2",
      "children": []
    }
  ],
  [
    {
      "name": "Occ Managerial Executive",
      "code": "Q050004",
      "parent": "EMP_16P_WHITE_COL",
      "value": "1748",
      "children": []
    },
    {
      "name": "Occ Prof Specialty",
      "code": "Q050010",
      "parent": "EMP_16P_WHITE_COL",
      "value": "2372",
      "children": []
    },
    {
      "name": "Occ Healthcare Support",
      "code": "Q050024",
      "parent": "EMP_16P_WHITE_COL",
      "value": "91",
      "children": []
    },
    {
      "name": "Occ Sales",
      "code": "Q050032",
      "parent": "EMP_16P_WHITE_COL",
      "value": "1323",
      "children": []
    },
    {
      "name": "Occ Office Admin",
      "code": "Q050033",
      "parent": "EMP_16P_WHITE_COL",
      "value": "623",
      "children": []
    }
  ],
  [
    {
      "name": "Occ Protective",
      "code": "Q050025",
      "parent": "EMP_16P_BLUE_COL",
      "value": 75,
      "children": []
    },
    {
      "name": "Occ Food Preparation Serving",
      "code": "Q050028",
      "parent": "EMP_16P_BLUE_COL",
      "value": 394,
      "children": []
    },
    {
      "name": "Occ Bldg Maintenance & Cleaning",
      "code": "Q050029",
      "parent": "EMP_16P_BLUE_COL",
      "value": 183,
      "children": []
    },
    {
      "name": "Occ Personal Care",
      "code": "Q050030",
      "parent": "EMP_16P_BLUE_COL",
      "value": 421,
      "children": []
    },
    {
      "name": "Occ Farming, Fishing & Forestry",
      "code": "Q050034",
      "parent": "EMP_16P_BLUE_COL",
      "value": 0,
      "children": []
    },
    {
      "name": "Occ Construction",
      "code": "Q050035",
      "parent": "EMP_16P_BLUE_COL",
      "value": 330,
      "children": []
    },
    {
      "name": "Occ Production Transportation",
      "code": "Q050041",
      "parent": "EMP_16P_BLUE_COL",
      "value": 404,
      "children": []
    }
  ],
  [
    {
      "name": "No Vehicle",
      "code": "Q029003",
      "parent": "VEHICLES_PER_HH",
      "value": "531",
      "children": []
    },
    {
      "name": "1 Vehicle",
      "code": "Q029004",
      "parent": "VEHICLES_PER_HH",
      "value": "2808",
      "children": []
    },
    {
      "name": "2 Vehicle",
      "code": "Q029005",
      "parent": "VEHICLES_PER_HH",
      "value": "2404",
      "children": []
    },
    {
      "name": "3 Vehicle",
      "code": "Q029006",
      "parent": "VEHICLES_PER_HH",
      "value": "555",
      "children": []
    },
    {
      "name": "4 Vehicle",
      "code": "Q029007",
      "parent": "VEHICLES_PER_HH",
      "value": "131",
      "children": []
    },
    {
      "name": "5 Vehicle",
      "code": "Q029008",
      "parent": "VEHICLES_PER_HH",
      "value": "53",
      "children": []
    },
    {
      "name": "Aggregate Number of Vehicle",
      "code": "Q029009",
      "parent": "VEHICLES_PER_HH",
      "value": "10078",
      "children": []
    }
  ]
];

var el = ["Q029009","Q017001","Q035001"];

verify("Found codes before removal:", arr, el); // should be `true`

arr = arr.map(a => a.filter(o => !el.includes(o.code)));

verify("Found codes after removal:", arr, el); // should be `false`

console.log(arr)

function verify(msg, arr, el) {
  console.log(msg, arr.some(a => a.some(o => el.includes(o.code))));
}