gpt4 book ai didi

javascript - 在 React.JS 中将映射函数嵌套在映射函数中

转载 作者:行者123 更新时间:2023-12-02 23:58:05 24 4
gpt4 key购买 nike

我正在尝试嵌套一个从 JSON 文件中提取数据的 map 函数。我的外部 map 函数执行良好。内部映射函数似乎抛出错误,说它不是“item.map 不是函数”,我想知道这是否只是一个语法错误或什么。下面是我的 Client.js 文件:

import React, { Component } from "react";
import { Link } from "react-router-dom";
import ClientList from "../Clients/ClientList";
import ClientListData from "../Clients/ClientListData";

class Client extends Component {
render() {
return (
<div>
{ClientListData.map(function(item, i) {
return (
<div className="column-wrap list half-green" key={i}>
<div className="column half title">
<h3>{item.bannerText}</h3>
</div>
<div className="column half details">
{item.map(function(innerItem, i) {
return (
<ClientList imgClass={innerItem.imgClass} imgSrc={innerItem.imgSrc} imgAlt={innerItem.imgAlt} imgText={innerItem.imgText} year={innerItem.year} />
);
})}
</div>
</div>
);
})}
</div>
)

}
}
export default Client;

我有一个名为 ClientList.js 的文件,我将其导入到 Client.js 中:

 import React, { Component } from "react";
import { Link } from "react-router-dom";
import ClientListData from "../Clients/ClientListData";

class ClientList extends Component {
render() {
return (
<div className="client-details">
<div className="logo-wrap">
<img className={this.props.imgClass} src={this.props.imgSrc} alt={this.props.imgAlt} />
</div>
<span className="client">
{this.props.imgText}
</span>
<ul>
<li>{this.props.year}</li>
</ul>
</div>
);
}
}
export default ClientList;

这是我的 JSON 文件,名为 ClientListData.json:

[
{
"bannerText": "Over $10 Million",
"theClients" :[
{
"imgClass": "logo pcb",
"imgSrc": "logos/PCB@2x.png",
"imgAlt": "Panama City Beach",
"imgText": "Panama City Beach",
"year": "2016 | AOR"
}
]
},

{
"bannerText": "$5 - 10 Million",
"theClients" :[
{
"imgClass": "logo pcb",
"imgSrc": "logos/PCB@2x.png",
"imgAlt": "Panama City Beach",
"imgText": "Panama City Beach",
"year": "2016 | AOR"
},
{
"imgClass": "logo pcb",
"imgSrc": "logos/PCB@2x.png",
"imgAlt": "Panama City Beach",
"imgText": "Panama City Beach",
"year": "2016 | AOR"
},
{
"imgClass": "logo pcb",
"imgSrc": "logos/PCB@2x.png",
"imgAlt": "Panama City Beach",
"imgText": "Panama City Beach",
"year": "2016 | AOR"
}
]
}
]

最佳答案

应该是:

item.theClients.map(function(innerItem, i) {

您缺少 theClients key 。

关于javascript - 在 React.JS 中将映射函数嵌套在映射函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com