简体   繁体   English

如何从 ReactJs 中的数组中过滤掉数据

[英]How to filter out data from array in ReactJs

I'm currently coding my portfolio website.我目前正在编写我的投资组合网站。 Inside each project page, there is a section above the footer that is basically a nav to endorse other projects.在每个项目页面内,页脚上方都有一个部分,基本上是用于支持其他项目的导航。

I'm new to react, and I was wondering if there was a way to find the current URL (just the extension) and exclude that from the projects being displayed?我是新手,我想知道是否有办法找到当前的 URL(只是扩展名)并将其从正在显示的项目中排除?

All my project data (link, title, name, description) are organized in an array, so theoretically it would be我所有的项目数据(链接、标题、名称、描述)都组织在一个数组中,所以理论上它会是

if (projectData.link != currentURL){
// pass the props
}

The MoreProjectCard is a component, code is below: MoreProjectCard 是一个组件,代码如下:

function MoreProjects() {
  const moreProjects = projectData.map((project) => (
    <MoreProjectCard
      id={project.name}
      key={project.name}
      link={project.link}
      title={project.title}
      description={project.description}
    />
  ));

  return (
    <div className="section padding-h">
      <h3 className="mb-50">View other projects</h3>
      {moreProjects}
    </div>
  );
}

I can provide more code if needed.如果需要,我可以提供更多代码。

Thank you!谢谢!

Update:更新:

projectData:项目数据:

export default [
  {
    name: 'doodlevr',
    title: '50 Years of VR',
    link: '/doodlevr',
    category: 'AR/VR, Front End Development',
    description: 'Reinventing the Google Doodle',
  },
  {
    name: 'bridge',
    title: 'Bridge',
    link: '/bridge',
    category: 'UX/UI Design, User Research',
    description: 'Fostering connections between students and mentors',
  },
  {
    name: 'stellar',
    title: 'Stellar',
    link: '/stellar',
    category: 'UX/UI Design',
    description: 'Empowering families through storytelling',
  },
];

an example URL would be www.roneilla.com/stellar例如 URL 将是 www.roneilla.com/stellar

I would be looking at filtering out "/stellar"我会考虑过滤掉“/stellar”

below is the code for the moreProjectCard component下面是 moreProjectCard 组件的代码

import React from 'react';
import { Link } from 'react-router-dom';
import { ReactComponent as Arrow } from './../assets/arrow.svg';

import './../Text.css';
import './../App.css';

function MoreProjectCard(props) {
  return (
    <div className="moreProjectCard padding-h" id={props.id}>
      <Link to={props.link}>
        <h4>{props.title}</h4>
        <p>{props.description}</p>
      </Link>
      <Arrow className="more-arrow-icon" />
    </div>
  );
}

export default MoreProjectCard;

Update更新

import React from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import { ReactComponent as Logo } from './assets/logo.svg';
import Navbar from './components/Navbar';
import Footer from './components/Footer';
import ScrollToTop from './ScrollToTop';

import CaseStudies from './pages/CaseStudies';
import About from './pages/About';

import Doodlevr from './pages/Doodlevr';

import './App.css';

function App() {
  return (
    <BrowserRouter>
      <ScrollToTop />
      <div className="app">
        <Navbar />
        <Route exact path="/" component={CaseStudies} />
        <Route exact path="/doodlevr" component={Doodlevr} />
        <Route exact path="/about" component={About} />
        <Footer />
      </div>
    </BrowserRouter>
  );
}

export default App;

If I understand your app structure, then each route renders a page component with a MoreProjects component at the bottom of the page.如果我了解您的应用程序结构,那么每个路由都会在页面底部呈现一个带有MoreProjects组件的页面组件。 This means MoreProjects is being rendered within a Route and has access to the route-props .这意味着MoreProjects正在Route中呈现并且可以访问route-props The route location pathname is what you need for comparisons.路线位置pathname是您进行比较所需的。

Since MoreProjects is a functional component you can use the useLocation hook to get this information.由于MoreProjects是一个功能组件,因此您可以使用useLocation挂钩来获取此信息。

So given URL www.roneilla.com/stellar , the current matching location.pathname is /steller .所以给定 URL www.roneilla.com/stellar ,当前匹配的location.pathname/steller

function MoreProjects() {
  const { pathname } = useLocation();

  const moreProjects = projectData
    .filter(({ link }) => link !== pathname) // <-- filter the link by current pathname
    .map((project) => (
      <MoreProjectCard
        id={project.name}
        key={project.name}
        link={project.link}
        title={project.title}
        description={project.description}
      />
    ));

  return (
    <div className="section padding-h">
      <h3 className="mb-50">View other projects</h3>
      {moreProjects}
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM