skip to Main Content
const columnDateIndex = [
  { path: "2023|Jan 2023" },
  { path: "2023|Feb 2023" },
  { path: "2023|Mar 2023" },
  { path: "2023|Apr 2023" },
  { path: "2023|May 2023" },
  { path: "2023|Jun 2023" },
  { path: "2023|Jul 2023" },
  { path: "2023|Aug 2023" },
  { path: "2023|Sep 2023" },
  { path: "2023|Oct 2023" },
  { path: "2023|Nov 2023" },
  { path: "2023|Dec 2023" },
  { path: "2024|Jan 2024" },
  { path: "2024|Feb 2024" },
  { path: "2024|Mar 2024" },
  { path: "2024|Apr 2024" },
  { path: "2024|May 2024" },
  { path: "2024|Jun 2024" },
  { path: "2024|Jul 2024" },
  { path: "2024|Aug 2024" },
  { path: "2024|Sep 2024" },
  { path: "2024|Oct 2024" },
  { path: "2024|Nov 2024" },
  { path: "2024|Dec 2024" },
];

const columnPath = "Williams|Bonus|2024|Feb 2024";

const result = columnDateIndex.find((column) => {
  const re = new RegExp(`${column.path}$`);
  return re.test(columnPath);
});

console.log(result);

Output:
{ path: ‘2024|Jan 2024’ }

Expected Output:
{ path: "2024|Feb 2024" }

I want to get the exact end match.

2

Answers


  1. I will use endsWith here:

    const columnDateIndex = [
      { path: "2023|Jan 2023" },
      { path: "2023|Feb 2023" },
      { path: "2023|Mar 2023" },
      { path: "2023|Apr 2023" },
      { path: "2023|May 2023" },
      { path: "2023|Jun 2023" },
      { path: "2023|Jul 2023" },
      { path: "2023|Aug 2023" },
      { path: "2023|Sep 2023" },
      { path: "2023|Oct 2023" },
      { path: "2023|Nov 2023" },
      { path: "2023|Dec 2023" },
      { path: "2024|Jan 2024" },
      { path: "2024|Feb 2024" },
      { path: "2024|Mar 2024" },
      { path: "2024|Apr 2024" },
      { path: "2024|May 2024" },
      { path: "2024|Jun 2024" },
      { path: "2024|Jul 2024" },
      { path: "2024|Aug 2024" },
      { path: "2024|Sep 2024" },
      { path: "2024|Oct 2024" },
      { path: "2024|Nov 2024" },
      { path: "2024|Dec 2024" },
    ];
    
    const columnPath = "Williams|Bonus|2024|Feb 2024";
    
    const result = columnDateIndex.find((column) => {
      return columnPath.endsWith(column.path);
    });
    
    console.log(result);
    Login or Signup to reply.
  2. you have to esacpe th | and space chars, they should be treated as literal chars

    const columnDateIndex = [
      { path: "2023|Jan 2023" },
      { path: "2023|Feb 2023" },
      { path: "2023|Mar 2023" },
      { path: "2023|Apr 2023" },
      { path: "2023|May 2023" },
      { path: "2023|Jun 2023" },
      { path: "2023|Jul 2023" },
      { path: "2023|Aug 2023" },
      { path: "2023|Sep 2023" },
      { path: "2023|Oct 2023" },
      { path: "2023|Nov 2023" },
      { path: "2023|Dec 2023" },
      { path: "2024|Jan 2024" },
      { path: "2024|Feb 2024" },
      { path: "2024|Mar 2024" },
      { path: "2024|Apr 2024" },
      { path: "2024|May 2024" },
      { path: "2024|Jun 2024" },
      { path: "2024|Jul 2024" },
      { path: "2024|Aug 2024" },
      { path: "2024|Sep 2024" },
      { path: "2024|Oct 2024" },
      { path: "2024|Nov 2024" },
      { path: "2024|Dec 2024" },
    ];
    
    const columnPath = "Williams|Bonus|2024|Feb 2024";
    
    const result = columnDateIndex.find((column) => {
      const pattern = column.path.replace('|', '\|').replace(' ', '\s');
      const re = new RegExp(`${pattern}$`);
      return re.test(columnPath);
    });
    
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search