Home > Enterprise >  How to filter an array of arrays in JavaScript
How to filter an array of arrays in JavaScript

Time:12-15

I have a dynamic array which looks like

var array = [
  [
    "Advance Payment",
    [
      {
        "key": "100001",
        "reason_name": "Tax ID is missing",
        "reason_category": "Advance Payment",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100002",
        "reason_name": "Bank account information mismatch",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      .
      .
      .,
      {
        "key": "100003",
        "reason_name": "Invoice Settings Error",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Parental Authorization",
    [
      {
        "key": "100004",
        "reason_name": "Missing form",
        "reason_category": "Parental Authorization",
        "status_state": "required",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2022-02-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Missing Credit Card Info",
    [
      {
        "key": "100005",
        "reason_name": "Invalid Credit Card Numnber",
        "reason_category": "Missing Credit Card Info",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100006",
        "reason_name": "Expired Credit Card",
        "reason_category": "Missing Credit Card Info",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100007",
        "reason_name": "Missing Signature on File",
        "reason_category": "Missing Credit Card Info",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100008",
        "reason_name": "Invalid CVV entered",
        "reason_category": "Missing Credit Card Info",
        "status_state": "required",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  .
  .
  .
  ,
  [
    "Missing Home Address",
    [
      {
        "key": "100009",
        "reason_name": "Invalid Postal Code",
        "reason_category": "Missing Home Address",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100010",
        "reason_name": "Missing Address Line 1",
        "reason_category": "Missing Home Address",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      .
      .
      .,
      {
        "key": "100011",
        "reason_name": "State Code Missing",
        "reason_category": "Missing State Code",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ]
]

I want to filter this array based on "status_state" === "complete" to return something like following:

var filteredResult = [
  [
    "Advance Payment",
    [
      {
        "key": "100002",
        "reason_name": "Bank account information mismatch",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100003",
        "reason_name": "Invoice Settings Error",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Missing Credit Card Info",
    [
      {
        "key": "100006",
        "reason_name": "Expired Credit Card",
        "reason_category": "Missing Credit Card Info",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Missing Home Address",
    [
      {
        "key": "100010",
        "reason_name": "Missing Address Line 1",
        "reason_category": "Missing Home Address",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100011",
        "reason_name": "State Code Missing",
        "reason_category": "Missing State Code",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ]
]

How do I achieve this?

I have tried

const filteredResult = array.filter((item) =>
item[1].some((subItem) => subItem.status === "complete"

This however also includes data that I do not want... the filteredResult looks like

[
  [
    "Advance Payment",
    [
      {
        "key": "100001",
        "reason_name": "Tax ID is missing",
        "reason_category": "Advance Payment",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100002",
        "reason_name": "Bank account information mismatch",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100003",
        "reason_name": "Invoice Settings Error",
        "reason_category": "Advance Payment",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Missing Credit Card Info",
    [
      {
        "key": "100005",
        "reason_name": "Invalid Credit Card Numnber",
        "reason_category": "Missing Credit Card Info",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100006",
        "reason_name": "Expired Credit Card",
        "reason_category": "Missing Credit Card Info",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100007",
        "reason_name": "Missing Signature on File",
        "reason_category": "Missing Credit Card Info",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100008",
        "reason_name": "Invalid CVV entered",
        "reason_category": "Missing Credit Card Info",
        "status_state": "required",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ],
  [
    "Missing Home Address",
    [
      {
        "key": "100009",
        "reason_name": "Invalid Postal Code",
        "reason_category": "Missing Home Address",
        "status_state": "pending",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100010",
        "reason_name": "Missing Address Line 1",
        "reason_category": "Missing Home Address",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      },
      {
        "key": "100011",
        "reason_name": "State Code Missing",
        "reason_category": "Missing State Code",
        "status_state": "complete",
        "update_at": "2022-04-08T22:22:49.355Z",
        "date_created": "2021-12-08T22:22:49.355Z"
      }
    ]
  ]
]

CodePudding user response:

Since you want to bring the keys with you, you can map the result and return it with the same structure but filter on the inner items.

const filteredResult = array.map(([key, nestedArray]) => [key, nestedArray.filter(item => item.status_state === "complete")]);

CodePudding user response:

I would filter twice to get the results you want. Once on the inner items (mapping the array), and once on the outer array:

array.map(([category, issues]) => [category, issues.filter(({status_state}) => status_state === "complete")]).filter(([, issues]) => issues.length);

CodePudding user response:

Here is a solution that leverages flatMap to do the map and filter on the given array all at once:

array.flatMap(([category, lines]) => {
  const completedLines = lines.filter(x => x.status_state === 'complete');
  if (completedLines.length === 0) {
    return [];
  } else {
    return [[category, completedLines]]; // Notice the double brackets for flatMap
  }
});

CodePudding user response:

Use reduce() so we can remove the complete array if the filter() does not find anything:

var array = [["Advance Payment", [{"key": "100001", "reason_name": "Tax ID is missing", "reason_category": "Advance Payment", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100002", "reason_name": "Bank account information mismatch", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100003", "reason_name": "Invoice Settings Error", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Parental Authorization", [{"key": "100004", "reason_name": "Missing form", "reason_category": "Parental Authorization", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2022-02-08T22:22:49.355Z"} ] ], ["Missing Credit Card Info", [{"key": "100005", "reason_name": "Invalid Credit Card Numnber", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100006", "reason_name": "Expired Credit Card", "reason_category": "Missing Credit Card Info", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100007", "reason_name": "Missing Signature on File", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100008", "reason_name": "Invalid CVV entered", "reason_category": "Missing Credit Card Info", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Missing Home Address", [{"key": "100009", "reason_name": "Invalid Postal Code", "reason_category": "Missing Home Address", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100010", "reason_name": "Missing Address Line 1", "reason_category": "Missing Home Address", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100011", "reason_name": "State Code Missing", "reason_category": "Missing State Code", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ] ]

const res = array.reduce((p, [ key, value ]) => {
    let filtered = value.filter(o => o.status_state === 'complete');
    return (filtered.length) ? [ ...p, [ key, filtered ] ] : p;
});

console.log(res)

CodePudding user response:

just map the array and return the key and the filtered array :

var array = [["Advance Payment", [{"key": "100001", "reason_name": "Tax ID is missing", "reason_category": "Advance Payment", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100002", "reason_name": "Bank account information mismatch", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100003", "reason_name": "Invoice Settings Error", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Parental Authorization", [{"key": "100004", "reason_name": "Missing form", "reason_category": "Parental Authorization", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2022-02-08T22:22:49.355Z"} ] ], ["Missing Credit Card Info", [{"key": "100005", "reason_name": "Invalid Credit Card Numnber", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100006", "reason_name": "Expired Credit Card", "reason_category": "Missing Credit Card Info", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100007", "reason_name": "Missing Signature on File", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100008", "reason_name": "Invalid CVV entered", "reason_category": "Missing Credit Card Info", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Missing Home Address", [{"key": "100009", "reason_name": "Invalid Postal Code", "reason_category": "Missing Home Address", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100010", "reason_name": "Missing Address Line 1", "reason_category": "Missing Home Address", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100011", "reason_name": "State Code Missing", "reason_category": "Missing State Code", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ] ]

const res = 
  array
    .map(([key, nestedArr]) => [key, nestedArr.filter((arr) => arr.status_state === 'complete')])
console.log(res)

  • Related