【问题标题】:How to use map within a map in javascript to access particular element如何在javascript中的地图中使用地图来访问特定元素
【发布时间】:2021-09-19 02:27:45
【问题描述】:

我有两个数组,一个是简单数组,另一个是对象数组。

这是数组:-

arr1=["aadhar", "address", "email", "mobile", "name", "pancard", "voterid"];
arr2=[ {
            "id": 21,
            "name": "johndoe",
            "email": "hello@gmail.com",
            "address": "test address",
            "voterid": "12131313",
            "mobile": "1211313",
            "aadhar": "213123131313",
            "pancard": "HYG579AA"
        },
        {
            "id": 24,
            "name": "johndoe3",
            "email": "hello1@gmail.com",
            "address": "test address",
            "voterid": "12111313",
            "mobile": "1211313",
            "aadhar": "112313131313",
            "pancard": "YHUIHU88"
        }];

我想在 arr1 中映射 arr2 以使用第一个 arr1 获取值。这是我尝试过的:

 {arr2.map((item) => {
              return (
                <Tr key={item.id}>
                  {arr1.map((itx) => {
                    return <Td>{item.itx}</Td>;
 })}
}

我希望项目像这样映射:-

item.aadhar
item.address
item.email
item.mobile

and so on...

但我无法在 dot 之后使用 itx 或 arr1 即 item.itx(未使用 itx)。

如果有什么方法可以告诉我。

【问题讨论】:

  • arr2.map(row =&gt; &lt;tr key={row.id}&gt;{arr1.map(item =&gt; &lt;td key={row.id + '_' + item}&gt;{row[item]}&lt;/td&gt;)}&lt;/tr&gt;)
  • 基本上你得到了正确的嵌套 map() 但你必须使用方括号表示法 (item[itx]) 而不是点表示法 (item.itx) 因为itx 是一个变量,而不是对象的属性名称。
  • item.itx 替换为item[itx]
  • 这能回答你的问题吗? Dynamically access object property using variable

标签: javascript arrays jsx array-map


【解决方案1】:

也许这更具可读性...

arr2.map(row => {
  return (
    <tr key={row.id}>
      {
        arr1.map(item => {
          return (
            <td key={`${row.id}_${item}`}>
              {row[item]}
            </td>
          )
        })
      }
    </tr>
  )
});

【讨论】:

    【解决方案2】:

    不要将数据操作逻辑与 React 组件布局逻辑纠缠在一起。写一个单独的pick(obj, fields)函数-

    const pick = (obj = {}, fields = []) =>
      fields.map(f => obj[f])
    
    const arr1 = ["aadhar", "address", "email", "mobile", "name", "pancard", "voterid"]
    const arr2 = [{id: 21,name: "johndoe",email: "hello@gmail.com",address: "test address",voterid: "12131313",mobile: "1211313",aadhar: "213123131313",pancard: "HYG579AA"},{id: 24,name: "johndoe3",email: "hello1@gmail.com",address: "test address",voterid: "12111313",mobile: "1211313",aadhar: "112313131313",pancard: "YHUIHU88"}]
      
    console.log(arr2.map(obj => pick(obj, arr1)))
    [
      [
        "213123131313",
        "test address",
        "hello@gmail.com",
        "1211313",
        "johndoe",
        "HYG579AA",
        "12131313"
      ],
      [
        "112313131313",
        "test address",
        "hello1@gmail.com",
        "1211313",
        "johndoe3",
        "YHUIHU88",
        "12111313"
      ]
    ]
    

    现在您可以轻松编写表格 -

    const rows = arr2.map(obj => pick(obj, arr1))
    return <table>
      {rows.map(row =>
        <tr>{row.map(cell => <td>...</td>)}</tr>
      )}
    </table>
    

    这是一个完整的示例,您可以在自己的浏览器中运行和验证 -

    const pick = (obj = {}, fields = []) =>
      fields.map(f => obj[f])
    
    function Table ({ rows, selector }) {
      return <table>
        {rows.map((row, key) =>
          <tr key={key}>
            {selector(row).map((cell, key) =>
              <td key={key}>{cell}</td>
            )}
          </tr>
        )}
      </table>
    }
    
    const arr1 = ["aadhar", "address", "email", "mobile", "name", "pancard", "voterid"]
    const arr2 = [{id: 21,name: "johndoe",email: "hello@gmail.com",address: "test address",voterid: "12131313",mobile: "1211313",aadhar: "213123131313",pancard: "HYG579AA"},{id: 24,name: "johndoe3",email: "hello1@gmail.com",address: "test address",voterid: "12111313",mobile: "1211313",aadhar: "112313131313",pancard: "YHUIHU88"}]
    
    ReactDOM.render(
      <Table rows={arr2} selector={x => pick(x, arr1)}/>,
      document.querySelector("main")
    )
    table { border: 1px solid black; }
    td { border: 1px solid silver; }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <main></main>

    这是一个包含列标题的替代示例 -

    const pick = (obj = {}, fields = []) =>
      fields.map(f => obj[f])
    
    function Table ({ rows, cols }) {
      return <table>
        <tr>
          {cols.map((col, key) =>
            <th key={key}>{col}</th>
          )}
        </tr>
        {rows.map((row, key) =>
          <tr key={key}>
            {pick(row, cols).map((cell, key) =>
              <td key={key}>{cell}</td>
            )}
          </tr>
        )}
      </table>
    }
    
    const arr1 = ["aadhar", "address", "email", "mobile", "name", "pancard", "voterid"]
    const arr2 = [{id: 21,name: "johndoe",email: "hello@gmail.com",address: "test address",voterid: "12131313",mobile: "1211313",aadhar: "213123131313",pancard: "HYG579AA"},{id: 24,name: "johndoe3",email: "hello1@gmail.com",address: "test address",voterid: "12111313",mobile: "1211313",aadhar: "112313131313",pancard: "YHUIHU88"}]
    
    ReactDOM.render(
      <Table rows={arr2} cols={arr1} />,
      document.querySelector("main")
    )
    table { border: 1px solid black; }
    th,td { border: 1px solid silver; }
    th { font-weight: bold; }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <main></main>

    【讨论】:

      猜你喜欢
      • 2015-01-24
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 2012-05-03
      相关资源
      最近更新 更多