0

I need to filter this array so it is only left with the correct category codes

const yearLayers= 
     [
        {
            "year": 2016,
            "eventNodes": [
               {"header": "Pearls of Wisdom", "categoryCode": 3,}
             ]
        },
        {
            "year": 2017,
            "eventNodes":[
              {"header": "VENuS Satellite", "categoryCode": 2},
              {"header": "Hope for millions", "categoryCode": 1}
            ]
        },
        {
            "year": 2012,
            "eventNodes": [
              {"header": "green electricity Pioneer", "categoryCode": 1}
              {"header": "This is a header", "categoryCode": 3,}
              {"header": "more titles here", "categoryCode": 1,}
            ]
        }
    ]

This is a snippet of what the array looks like. I need to filter out the objects with the category code that does not === 1. So I left with an array with only categoryCode: 1.

const yearLayers= 
     [
        {
            "year": 2016,
            "eventNodes": []
        },
        {
            "year": 2017,
            "eventNodes":[
              {"header": "Hope for millions", "categoryCode": 1}
            ]
        },
        {
            "year": 2012,
            "eventNodes": [
              {"header": "green electricity Pioneer", "categoryCode": 1}
              {"header": "more titles here", "categoryCode": 1,}
            ]
        }
    ]

I have tried to use array methods, but I cannot seem to reach inside the nested array and the nested objects. I think I am missing something

const filteredArr = yearLayers
            .map(layer => layer.eventNodes)
            .map(node => node.categoryCode)
            .filter(node => node.categoryCode !== 1)

2 Answers 2

3

You can use map() in combination with filter() like this:

const formattedWorkbook = [{
    "year": 2016,
    "value": [
       {"header": "Pearls of Wisdom", "categoryCode": 3}
     ]
}, {
    "year": 2017,
    "value":[
      {"header": "VENuS Satellite", "categoryCode": 2},
      {"header": "Hope for millions", "categoryCode": 1}
    ]
}, {
    "year": 2012,
    "value": [
      {"header": "green electricity Pioneer", "categoryCode": 1},
      {"header": "This is a header", "categoryCode": 3},
      {"header": "more titles here", "categoryCode": 1}
    ]
}];

const filter = (data, category) => data.map(({year, value}) => ({
  year,
  eventNodes: value.filter(v => v.categoryCode == category)
}));

console.log(filter(formattedWorkbook, 1));

This avoids modifying the original data.

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily get the result if you use map in the combination with a filter.

const formattedWorkbook = [
  {
    year: 2016,
    value: [{ header: "Pearls of Wisdom", categoryCode: 3 }],
  },
  {
    year: 2017,
    value: [
      { header: "VENuS Satellite", categoryCode: 2 },
      { header: "Hope for millions", categoryCode: 1 },
    ],
  },
  {
    year: 2012,
    value: [
      { header: "green electricity Pioneer", categoryCode: 1 },
      { header: "This is a header", categoryCode: 3 },
      { header: "more titles here", categoryCode: 1 },
    ],
  },
];

const result = formattedWorkbook.map(({ year, value }) => {
  const filteredValue = value.filter((o) => o.categoryCode === 1);
  return { year, value: filteredValue };
});

console.log(result);

2)

const formattedWorkbook = [
  {
    year: 2016,
    value: [{ header: "Pearls of Wisdom", categoryCode: 3 }],
  },
  {
    year: 2017,
    value: [
      { header: "VENuS Satellite", categoryCode: 2 },
      { header: "Hope for millions", categoryCode: 1 },
    ],
  },
  {
    year: 2012,
    value: [
      { header: "green electricity Pioneer", categoryCode: 1 },
      { header: "This is a header", categoryCode: 3 },
      { header: "more titles here", categoryCode: 1 },
    ],
  },
];

const result = formattedWorkbook.map(({ year, value }) => ({
  year,
  value: value.filter((o) => o.categoryCode === 1),
}));

console.log(result);

2 Comments

You've mutated the original objects, which is not necessarily a good idea - it might not make a difference but can often lead to unintended side effects
Just FYI - answers which are code only with no explanation as to what has been done or what has been changed are less useful for future visitors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.