什么是从JSON对象数组中填充ReactJS选择字段的最简单方法?

huangapple go评论68阅读模式
英文:

What's the easiest way to populate a ReactJS select field with user names from a JSON object array?

问题

我正在尝试创建我的第一个React js项目,并且需要一些关于选择选项的帮助。
所以,我有这个JSON数据文件:

{
  "users": [
    {
      "id": "1",
      "name": "Anton Tur",
      "username": "nosensejk"   
    },
    {
      "id": "2",
      "name": "Peggy Sonaya",
      "username": "Peggaya"      
    },
    {
      "id": "3",
      "name": "Joseph Maguire",
      "username": "Joemag"      
    }
  ],
  "transports": [
    {
      "id": "T1",
      "name": "MAZ 2105",
      "type": "bus"     
    },
    {
      "id": "T2",
      "name": "MAZ 2106",
      "type": "bus"     
    },
    {
      "id": "T3",
      "name": "MAZ 2205",
      "type": "trolleybus"      
    },
    {
      "id": "T4",
      "name": "MAZ 2307",
      "type": "tram"    
    }
  ]
}

可以有人向我展示如何使用选择字段来显示用户的名称的最简单方法吗?提前感谢。

以下是我的JS文件中的一些代码:

import React from 'react';
import db from './../../node_modules/server/db.json';

function calculate() {
  return (
    <div>
      <select>
        <option value="choose" disabled defaultValue>
          -- 选择用户 --
        </option>
        {db.users.map((user) => (
          <option key={user.id} value={user.id}>
            {user.name}
          </option>
        ))}
      </select>
    </div>
  );
}

export default calculate;
英文:

I'm trying to create my first project in React js and I need some help with select option.
So, I have this JSON data file:

{
  &quot;users&quot;: [
    {
      &quot;id&quot;: &quot;1&quot;,
      &quot;name&quot;: &quot;Anton Tur&quot;,
      &quot;username&quot;: &quot;nosensejk&quot;   
    },
    {
      &quot;id&quot;: &quot;2&quot;,
      &quot;name&quot;: &quot;Peggy Sonaya&quot;,
      &quot;username&quot;: &quot;Peggaya&quot;      
    },
    {
      &quot;id&quot;: &quot;3&quot;,
      &quot;name&quot;: &quot;Joseph Maguire&quot;,
      &quot;username&quot;: &quot;Joemag&quot;      
    }
  ],
  &quot;transports&quot;: [
    {
      &quot;id&quot;: &quot;T1&quot;,
      &quot;name&quot;: &quot;MAZ 2105&quot;,
      &quot;type&quot;: &quot;bus&quot;     
    },
    {
      &quot;id&quot;: &quot;T2&quot;,
      &quot;name&quot;: &quot;MAZ 2106&quot;,
      &quot;type&quot;: &quot;bus&quot;     
    },
    {
      &quot;id&quot;: &quot;T3&quot;,
      &quot;name&quot;: &quot;MAZ 2205&quot;,
      &quot;type&quot;: &quot;trolleybus&quot;      
    },
    {
      &quot;id&quot;: &quot;T4&quot;,
      &quot;name&quot;: &quot;MAZ 2307&quot;,
      &quot;type&quot;: &quot;tram&quot;,    
    }
  ]
}

Can anyone show me the easiest way to use a select field to display, for example, users' names? Thank you in advance.

Here is some code in my JS file

import React from &#39;react&#39;;
import db from &#39;./../../node_modules/server/db.json&#39;;


function calculate(){


  return(
  &lt;div&gt;
     &lt;select&gt;
      &lt;option value=&quot;choose&quot; disabled selected=&quot;selected&quot;&gt;
         -- Select user --
      &lt;/option&gt;
      {db.users.name}
      &lt;/select&gt;
   &lt;/div&gt;
  );
}
export default calculate;

答案1

得分: 1

const data = {
  users: [
    {
      id: "1",
      name: "Anton Tur",
      username: "nosensejk"
    },
    {
      id: "2",
      name: "Peggy Sonaya",
      username: "Peggaya"
    },
    {
      id: "3",
      name: "Joseph Maguire",
      username: "Joemag"
    }
  ],
  transports: [
    {
      id: "T1",
      name: "MAZ 2105",
      type: "bus"
    },
    {
      id: "T2",
      name: "MAZ 2106",
      type: "bus"
    },
    {
      id: "T3",
      name: "MAZ 2205",
      type: "trolleybus"
    },
    {
      id: "T4",
      name: "MAZ 2307",
      type: "tram"
    }
  ]
};
export default function Calculate() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <select>
        {data.users.map((user) => {
          return <option key={user.id} value={user.id}>{user.name}</option>;
        })}
      </select>
    </div>
  );
}

请注意,组件名称应以大写字母开头,例如 Calculate

英文:
const data = {
  users: [
    {
      id: &quot;1&quot;,
      name: &quot;Anton Tur&quot;,
      username: &quot;nosensejk&quot;
    },
    {
      id: &quot;2&quot;,
      name: &quot;Peggy Sonaya&quot;,
      username: &quot;Peggaya&quot;
    },
    {
      id: &quot;3&quot;,
      name: &quot;Joseph Maguire&quot;,
      username: &quot;Joemag&quot;
    }
  ],
  transports: [
    {
      id: &quot;T1&quot;,
      name: &quot;MAZ 2105&quot;,
      type: &quot;bus&quot;
    },
    {
      id: &quot;T2&quot;,
      name: &quot;MAZ 2106&quot;,
      type: &quot;bus&quot;
    },
    {
      id: &quot;T3&quot;,
      name: &quot;MAZ 2205&quot;,
      type: &quot;trolleybus&quot;
    },
    {
      id: &quot;T4&quot;,
      name: &quot;MAZ 2307&quot;,
      type: &quot;tram&quot;
    }
  ]
};
export default function Calculate() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;h1&gt;Hello CodeSandbox&lt;/h1&gt;
      &lt;h2&gt;Start editing to see some magic happen!&lt;/h2&gt;
      &lt;select&gt;
        {data.users.map((user) =&gt; {
          return &lt;option key={user.id} value={user.id}&gt;{user.name}&lt;/option&gt;;
        })}
      &lt;/select&gt;
    &lt;/div&gt;
  );
}

Please keep in mind that component names should start with a capital such as Calculate

huangapple
  • 本文由 发表于 2023年5月24日 20:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定