简体   繁体   English

React Js map function 给出“不是函数”错误

[英]React Js map function gives an “not a function” error

I'm trying to map through objects to display their values in my React JS project.我正在尝试通过对象 map 在我的 React JS 项目中显示它们的值。 It looks like I can't access values of the objects within objects using the map function, values simply are not displayed, or if I try to use (.split("/p")) it gives me an error saying "split is not a function".看起来我无法使用 map function 访问对象内对象的值,根本不显示值,或者如果我尝试使用 (.split("/p")) 它给我一个错误,说“split is不是函数”。

Link to codesandbox 链接到代码框

this is my code:这是我的代码:

import React from "react";
import { studies } from "./studies";

import "./styles.css";

const Data = ({ title, text, number, numberdesc }) => {
  return (
    <>
      <div style={{ margin: "1rem" }}>{title}</div>
      <div style={{ margin: "1rem" }}>{text}</div>
      <div>{number}</div>
      <div>{numberdesc}</div>
    </>
  );
};

function App() {
  const appComponent = studies.map((post, i) => {
    console.log(studies);
    return (
      <Data
        key={i}
        number={studies[i].post.number}
        title={studies[i].post.title}
        text={studies[i].post.text
          .split("/p")
          .reduce((total, line) => [total, <br />, <br />, line])}
      />
    );
  });
  return <>{appComponent}</>;
}

export default App;

and {studies} file:{studies}文件:

export const studies = [
  {
    id: "first",
    text: "1st Post",
    post: {
      data: [
        { number: "100", numberdesc: "description1" },
        { number: "200", numberdesc: "description2" },
        { number: "300", numberdesc: "description3" }
      ],

      text: [
        {
          title: "Title1 from the 1st post",
          text: "Text1 from the 1st post."
        },
        {
          title: "Title2 from the 1st post",
          text: "Text2 from the 1st post."
        },
        {
          title: "Title3 from the 1st post",
          text: "Text3 from the 1st post"
        }
      ]
    }
  },
  {
    id: "second",
    text: "2nd Post",
    post: {
      data: [
        { number: "100", numberdesc: "description1" },
        { number: "200", numberdesc: "description2" },
        { number: "300", numberdesc: "description3" }
      ],

      text: [
        {
          title: "Title1 from the 2nd post",
          text: "Text1 from the 2nd post "
        },
        {
          title: "Title2 from the 2nd post",
          text: "Text2 /p from the 2nd post"
        },
        {
          title: "Title3 from the 2nd post",
          text: "Text3 from the 2nd post"
        }
      ]
    }
  }
];

What I want to do is to access data and text values for each post, and display them in my Project.我想要做的是访问每个帖子的数据和文本值,并将它们显示在我的项目中。 Any help and suggestion is greatly appreciated,非常感谢任何帮助和建议,

Thank you.谢谢你。

I think you may be wanting to .map your array of content.我认为您可能想要.map您的内容数组。 For example:例如:

text={studies[i].post.text.map(t => <p><strong>{t.title}</strong>: {t.text}</p>)}

might replace the existing line that is breaking.可能会替换正在中断的现有行。

Is this what you're looking for?这是你要找的吗?

function App() {
  const appComponent = studies.map(study =>
    study.post.data.map((data, k) => (
      <Data
        key={k}
        number={data.number}
        numberdesc={data.numberdesc}
        title={study.post.text[k].title}
        text={study.post.text[k].text}
      />
    ))
  );
  return <>{appComponent}</>;
}

Note I arbitrarily zipped data[k] 's number and numberdesc with text[k] 's title and text , but that might not necessarily be what you intend to display.请注意,我使用text[k]titletext任意压缩data[k]numbernumberdesc ,但这可能不一定是您想要显示的内容。

The above will likely break in case your data and text arrays do not have the same length in any given study .如果您的datatext arrays 在任何给定study中的长度不同,上述内容可能会中断。

See it here .这里

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

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