简体   繁体   English

如何通过不同的json映射并从数组获取数据

[英]How to map through different jsons and get data from an array

I have created 2 different jsons and I need to get the courseName from both jsons using one function and populate them in a dropdown 我创建了2个不同的json,我需要使用一个函数从两个json中获取courseName,并将其填充到下拉列表中

I am currently able to do this using a map function on both jsons in separate option tags within an input element 我目前能够在输入元素内的单独选项标签中的两个json上使用map函数来执行此操作

import React from 'react'
import MesaLinda from './MesaLinda.json';
import TijerasCreek from './TijerasCreek.json';
import { Form, FormGroup, Label, Input, Button } from 'reactstrap'
import '../App.css';


const course = (props) => {
    return (
        <Form>
            <FormGroup>
                <Label for="courseSelect">Course</Label>
                <Input type="select" name="select" id="courseSelect" >
                    {MesaLinda.CostaMesaCCMesaLinda.map((mesaLinda, index) => {
                        return <option>
                            {mesaLinda.courseName}
                        </option>
                    })}
                    {TijerasCreek.TijerasCreek.map((tijerasCreek, index) => {
                        return <option>
                            {tijerasCreek.courseName}
                        </option>
                    })}
                </Input>
            </FormGroup>
            <FormGroup>

I want to be able to use one map function to iterate through multiple different json files to get the courseName 我希望能够使用一个map函数来遍历多个不同的json文件来获取courseName

You can do something like: 您可以执行以下操作:

import React from 'react'
import MesaLinda from './MesaLinda.json';
import TijerasCreek from './TijerasCreek.json';
import { Form, FormGroup, Label, Input, Button } from 'reactstrap'
import '../App.css';


const course = (props) => {
    return (
        <Form>
            <FormGroup>
                <Label for="courseSelect">Course</Label>
                <Input type="select" name="select" id="courseSelect" >
                    {[
                       ...MesaLinda.CostaMesaCCMesaLinda, 
                       ...TijerasCreek.TijerasCreek
                        ].map((json, index) => {
                        return <option>
                            {json.courseName}
                        </option>
                    })}
                </Input>
            </FormGroup>
         <FormGroup>

Concatenating two arrays using the spread operator, and then calling the same map function on it. 使用传播运算符连接两个数组,然后在其上调用相同的映射函数。

kasperlauge's answer is definitely great. 卡巴斯拉格的答案肯定是伟大的。 If you're indeed planning on adding more and more jsons I'd suggest perhaps adding a function like this one outside of your current, and then calling it with an array of the jsons: 如果您确实打算添加更多的json,那么建议您在当前代码之外添加一个类似这样的函数,然后使用json数组进行调用:

const jsonOptions = ( json ) => {
  return json.map( ( { courseName } ) => {
    return <options>
      { courseName }
    </options>;
  } );
};

const course = ( props ) => {
...
  <Input type="select" name="select" id="courseSelect">
    jsonOptions( [ 
      ...MesaLinda.CostaMesaCCMesaLinda, 
      ...TijerasCreek.TijerasCreek 
    ] )
  </Input>
...

It's just an alternative, I think it would scale better. 这只是一种选择,我认为它将更好地扩展。 ( Also note the destructuring inside the map(), it's very useful. ) (还要注意map()内部的解构,这非常有用。)

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

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