0

So here's my data, I need to reduce? or filter it based on the given search string.

const contents = [
  {
    title: "Accounts",
    links: [
      {
        header: "Accounts by Status",
      },
    ],
  },
  {
    title: "Executions",
    links: [
      {
        header: "Purchase and Sales",
      },
      {
        header: "AMLA Transactions Proof List",
      },
      {
        header: "Account Ranking",
      },
      {
        header: "Trading Summary",
      },
    ],
  },
];

const search = "account";

console.log(
  contents.filter((content) =>
    content.links.some((link) =>
      link.header.toLowerCase().includes(search.toLowerCase())
    )
  )
);
.as-console-wrapper { max-height: 100% !important; }

Can someone guide me in the right direction? I think reduce with filter can do the job but I don't know where to start. Thanks!

If my search string is 'account' My desired output should be something like this

[{
    title: 'Accounts',
    links: [{
      header: 'Accounts by Status'
    }]
  },
  {
    title: 'Executions',
    links: [{
      header: 'Account Ranking'
    }]
  }
]
2
  • 1
    you only want to filter the header in links? what'll happen if i search "Trading"? Commented Jan 5, 2023 at 8:28
  • 1
    So does your current "solution" work or not? I'm confused because you call it a solution and then ask where to start. Commented Jan 5, 2023 at 8:29

2 Answers 2

3

You need to rebuild content with links, if they match.

const
    contents = [{ title: 'Accounts', links: [{ header: 'Accounts by Status' }] }, { title: 'Executions', links: [{ header: 'Purchase and Sales' }, { header: 'AMLA Transactions Proof List' }, { header: 'Account Ranking' }, { header: 'Trading Summary' }] }], 
    search = 'account',
    result = contents.flatMap(content => {
        const links = content.links.filter(({ header }) => header
            .toLowerCase()
            .includes(search.toLowerCase())
        );
        return links.length
            ? { ...content, links }
            : [];
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

This solution filters the links array by search string, and if non is found removes the item all together. The test input contains one more item that produces no hit: { title: 'No hit', links: [{ header: 'Something else' }] }

const input = [
  { title: 'Accounts', links: [{ header: 'Accounts by Status' }] },
  { title: 'Executions', links: [{ header: 'Purchase and Sales' }, { header: 'AMLA Transactions Proof List' }, { header: 'Account Ranking' }, { header: 'Trading Summary' }] },
  { title: 'No hit', links: [{ header: 'Something else' }] }
];
const searchString = 'account';

// for performance perform lowercase once ahead of time:
const search = searchString.toLowerCase();
const result = input.map(obj => {
  let arr = obj.links.filter(o => o.header.toLowerCase().includes(search));
  if(arr.length) {
    // don't change original input, but return a filtered copy
    return {
      title: obj.title,
      links: arr
    };
  } else {
    return null;
  }
}).filter(Boolean);
console.log(result);

Comments

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.