如何在 javascript 中的地图中使用地图来访问特定元素
How to use map within a map in javascript to access particular element
我有两个数组,一个是简单数组,另一个是对象数组。
这是数组:-
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...
但是我无法在点之后使用 itx 或 arr1
即 item.itx(未使用 itx)。
如果有任何方法可以做到这一点,请告诉我。
也许这样更易读...
arr2.map(row => {
return (
<tr key={row.id}>
{
arr1.map(item => {
return (
<td key={`${row.id}_${item}`}>
{row[item]}
</td>
)
})
}
</tr>
)
});
不要将数据操作逻辑与 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"
]
]
现在您可以轻松编写 table -
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>
这是一个替代示例,其中包含列 headers -
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>
我有两个数组,一个是简单数组,另一个是对象数组。
这是数组:-
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...
但是我无法在点之后使用 itx 或 arr1 即 item.itx(未使用 itx)。
如果有任何方法可以做到这一点,请告诉我。
也许这样更易读...
arr2.map(row => {
return (
<tr key={row.id}>
{
arr1.map(item => {
return (
<td key={`${row.id}_${item}`}>
{row[item]}
</td>
)
})
}
</tr>
)
});
不要将数据操作逻辑与 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"
]
]
现在您可以轻松编写 table -
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>
这是一个替代示例,其中包含列 headers -
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>