我如何将URL添加到数组中的对象

时间:2019-04-24 01:41:53

标签: javascript reactjs

我正在尝试将代码中的某些文本转换为URL,但是该标记不起作用。有人可以帮我吗?

我想让对象中的数据成为可点击的超链接

例如,我希望代码和实时单词成为超级链接

或类似的东西

`{ url: 'http://someurl.com', linkContents: 'this is what goes inside a link tag' }`


then just `<a href={teacher.url}>{teacher.linkContents}</a>`

尝试使用标签

const Teachers = () => {
 let teachers = TeacherList.map((teacher) => {
return (
  <li className="teacher" key={teacher.id} >
    <img className="teacher-img" src={teacher.img_src} alt="teacher" />
    <h3>{teacher.name}</h3>

    <p>{teacher.bio}</p>
    <ul className="skills">
      <li>HTML</li>
      <li>CSS</li>
      <li>Vanilla Javascript</li>


    </ul>
  </li>

const TeacherList = [
  {
    name: "Catharsis Code Live" ,
    bio: "Angie is a web developer and teacher who is passionate about building scalable, data driven web apps, especially ones that address old problems with new tech!",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-1"
  },
  {
    name: "NodeStradamus",
    bio: "'NodeStra' is a software engineer and philosopher trying to leave the world better than he found it. He codes for non-profits, eCommerce, and large-scale web apps.",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-2"
  },
  {
    name: "Geo 'Lo' Cation",
    bio: "Geo is a JavaScript developer working on large-scale applications. He's also a teacher who strives to support students in removing all barriers to learning code.",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-3"
  },
  {
    name: "Ecma Scriptnstuff",
    bio: "Ecma found her passion for computers and programming over 15 years ago. She is excited to introduce people to the wonderful world of JavaScript.",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-4"
  },

我希望代码和实时文本成为超级链接

2 个答案:

答案 0 :(得分:0)

您可以将两个键都添加到教师数组,并在anchor标签中使用它,如下所示:

const TeacherList = [
  {
    name: "Catharsis Code Live" ,
    bio: "Angie is a web developer and teacher who is passionate about building scalable, data driven web apps, especially ones that address old problems with new tech!",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-1",
    someUrl: "http://example.com"
  },
  {
    name: "NodeStradamus",
    bio: "'NodeStra' is a software engineer and philosopher trying to leave the world better than he found it. He codes for non-profits, eCommerce, and large-scale web apps.",
    img_src: "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-2",
    someUrl: "http://example.com"
  },
]

const Teachers = () => {
  let teachers = TeacherList.map(teacher => {
    return (
      <li className="teacher" key={teacher.id}>
        <a href={teacher.someUrl}>
          <h3>{teacher.name}</h3>
        </a>
        <img className="teacher-img" src={teacher.img_src} alt="teacher-img" />

        <p>{teacher.bio}</p>
        <ul className="skills">
          <li>HTML</li>
          <li>CSS</li>
          <li>Vanilla Javascript</li>
        </ul>
      </li>
    );
  });
  return teachers;
};

这里是running demo

答案 1 :(得分:0)

您可以简单地将URL放在TeachersList数组对象中,并将其与教师姓名一起呈现。

喜欢这个。

const TeacherList = [
  {
    name: "Catharsis Code Live",
    bio:
      "Angie is a web developer and teacher who is passionate about building scalable, data driven web apps, especially ones that address old problems with new tech!",
    img_src:
      "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-1",
    toUrl: "https://stackoverflow.com/users/7194437/prabu-samvel",
    urlText: "stackoverflow-prabusamvel"
  },
  {
    name: "NodeStradamus",
    bio:
      "'NodeStra' is a software engineer and philosopher trying to leave the world better than he found it. He codes for non-profits, eCommerce, and large-scale web apps.",
    img_src:
      "https://cdn1.imggmi.com/uploads/2019/4/24/898316d49dcd721476a0a6ebfd6bef9d-full.png",
    id: "teach-2",
    toUrl: "https://stackoverflow.com/users/9084093/marcin-pachole",
    urlText: "stackoverflow-marncinpachole"
  }
];

function Teachers() {
  let teachers = TeacherList.map(teacher => {
    return (
      <li className="teacher" key={teacher.id}>
        <img className="teacher-img" src={teacher.img_src} alt="teacher" />
        <h3>{teacher.name}</h3>
        <a href={teacher.toUrl}>
           <h4>{teacher.textUrl}</h4>
        </a> 
        <p>{teacher.bio}</p>
        <ul className="skills">
          <li>HTML</li>
          <li>CSS</li>
          <li>Vanilla Javascript</li>
        </ul>
      </li>
    );
  });

  return <div>{teachers}</div>;
}

fiddle在这里,工作demo