2 votes

Comment filtrer les objets d'un tableau en javascript ?

J'ai un tableau avec des objets et j'essaie de trouver comment filtrer quelques objets dont je n'ai pas besoin. J'essaie de les filtrer sur la base du champ. code . Voici le code que j'ai essayé jusqu'à présent et qui génère une erreur. a.filter is not a function . Que dois-je faire dans ce cas ? Le filtrage n'est-il pas la bonne façon de procéder ? merci.

var chartsArray = [
  {
    "name": "Total Educated",
    "code": "Q035001",
    "parent": "EDU_ATTAINMENT",
    "value": "9900",
    "label": "Total Educated",
    "children": []
  },
  {
    "name": "Grade  Less than 9",
    "code": "Q035003",
    "parent": "EDU_ATTAINMENT",
    "value": "369",
    "label": "Grade  9",
    "children": []
  },
  {
    "name": "Grade 9 to 12",
    "code": "Q035007",
    "parent": "EDU_ATTAINMENT",
    "value": "595",
    "label": "Grade 9 - 12",
    "children": []
  },
  {
    "name": "High School",
    "code": "Q035011",
    "parent": "EDU_ATTAINMENT",
    "value": "1174",
    "label": "High School",
    "children": []
  },
  {
    "name": "Some College",
    "code": "Q035012",
    "parent": "EDU_ATTAINMENT",
    "value": "1904",
    "label": "Some College",
    "children": []
  },
  {
    "name": "College Degree -  Associate's",
    "code": "Q035014",
    "parent": "EDU_ATTAINMENT",
    "value": "436",
    "label": "Associate's",
    "children": []
  },
  {
    "name": "College Degree -  Bachelor's",
    "code": "Q035015",
    "parent": "EDU_ATTAINMENT",
    "value": "2999",
    "label": "Bachelor's",
    "children": []
  },
  {
    "name": "College Degree -  Master's",
    "code": "Q035016",
    "parent": "EDU_ATTAINMENT",
    "value": "1763",
    "label": "Master's",
    "children": []
  },
  {
    "name": "College - Professional",
    "code": "Q035017",
    "parent": "EDU_ATTAINMENT",
    "value": "413",
    "label": "Professional",
    "children": []
  },
  {
    "name": "College Degree - Doctorate",
    "code": "Q035018",
    "parent": "EDU_ATTAINMENT",
    "value": "246",
    "label": "Doctorate",
    "children": []
  },
  {
    "name": "Enrollments (Total Population)",
    "code": "EDU_Enrollments",
    "parent": "EDU_ATTAINMENT",
    "label": "Enrollments (Total Population)",
    "children": [
      {
        "name": "Nursery school/Preschool",
        "code": "Q036003",
        "parent": "EDU_Enrollments",
        "value": "269",
        "label": "Nursery school/Preschool",
        "children": []
      },
      {
        "name": "Kindergarten/Elementary school",
        "code": "Q036006",
        "parent": "EDU_Enrollments",
        "value": "1156",
        "label": "Kindergarten/Elementary school",
        "children": []
      },
      {
        "name": "High School",
        "code": "Q036015",
        "parent": "EDU_Enrollments",
        "value": "539",
        "label": "High School",
        "children": []
      },
      {
        "name": "College/Graduate /Professional school",
        "code": "Q036018",
        "parent": "EDU_Enrollments",
        "value": "1869",
        "label": "College/Graduate /Professional school",
        "children": []
      },
      {
        "name": "Not Enrolled",
        "code": "Q036024",
        "parent": "EDU_Enrollments",
        "value": "10380",
        "label": "Not Enrolled",
        "children": []
      }
    ]
  },
  {
    "name": "Percents",
    "code": "PCT_EDU_ATTAINMENT",
    "parent": "EDU_ATTAINMENT",
    "label": "Percents",
    "children": [
      {
        "name": "% Grade  Less than 9",
        "code": "XQ035003",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "3.7231",
        "label": "% Grade  9",
        "children": []
      },
      {
        "name": "% Grade 9 to 12",
        "code": "XQ035007",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "6.0112",
        "label": "% Grade 9 - 12",
        "children": []
      },
      {
        "name": "% High school",
        "code": "XQ035011",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "11.8622",
        "label": "% High school",
        "children": []
      },
      {
        "name": "% Some college",
        "code": "XQ035012",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "19.2332",
        "label": "% Some college",
        "children": []
      },
      {
        "name": "% College - Associate",
        "code": "XQ035014",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "4.4073",
        "label": "Associate",
        "children": []
      },
      {
        "name": "% College - Bachelors",
        "code": "XQ035015",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "30.2966",
        "label": "Bachelors",
        "children": []
      },
      {
        "name": "% College - Masters",
        "code": "XQ035016",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "17.8096",
        "label": "Masters",
        "children": []
      },
      {
        "name": "% College - Professional",
        "code": "XQ035017",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "4.168",
        "label": "Professional",
        "children": []
      },
      {
        "name": "% College - Doctorate",
        "code": "XQ035018",
        "parent": "PCT_EDU_ATTAINMENT",
        "value": "2.4888",
        "label": "Doctorate",
        "children": []
      }
    ]
  }
];

var el=[
   "Q035001",
   "PCT_EDU_ATTAINMENT"
];

output = chartsArray = chartsArray.map(a => a.filter(function code(o) {
    if (!el.includes(o.code)) {
        if (o.children) {
            o.children = o.children.filter(code);
        }
        return true;
    }
}));

console.log(output)

2voto

janos Points 22603

chartsArray est un tableau d'objets. Lorsque vous faites chartsArray.map(a => a.filter(...)) , cela signifie que pour chaque objet a dans le tableau, invoquez filter . Ce qui est incorrect puisque les objets n'ont pas de filter méthode.

Il semble que vous vouliez utiliser filter sur le tableau, au lieu de map :

output = chartsArray = chartsArray.filter(function code(o) {
    if (!el.includes(o.code)) {
        if (o.children) {
            o.children = o.children.filter(code);
        }
        return true;
    }
});

0voto

Jonas Wilms Points 52419

Vous pouvez utiliser la récursion pour filtrer de manière récursive :

const filter = [
  "Q035001",
  "PCT_EDU_ATTAINMENT"
];

function filterBy(arr, filter){
  return arr.filter( obj => {
    obj.children = filterBy(obj.children, filter);
    return !filter.includes(obj code);
  });
}

const output =  filterBy(chartsArray, filter);

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X