Javascript 表中的反向排序
我有一个函数,可以用渐变的Javascript 表中的反向排序,javascript,reactjs,Javascript,Reactjs,我有一个函数,可以用渐变的id呈现表行。如何反转它?(第一行带有id=n,最后一行带有1) 这是我的代码 renderRows = () => { const { receipts } = this.props const rows = receipts.map((receipt, index) => ( <tr key={index}> <td>{receipt.id}</td> <
id
呈现表行。如何反转它?(第一行带有id=n
,最后一行带有1)
这是我的代码
renderRows = () => {
const { receipts } = this.props
const rows = receipts.map((receipt, index) => (
<tr key={index}>
<td>{receipt.id}</td>
<td >{receipt.organization.name}</td>
<td>{receipt.receiptNumber}</td>
<td>{receipt.series}</td>
<td>{receipt.customer.fname}</td>
<td>{receipt.customer.lname}</td>
<td>{receipt.customer.patronymic}</td>
<td>{receipt.customer.phone}</td>
<td>
{receipt.priceList.map((priceListEntry, index) =>
<div key={index}>
{priceListEntry.service.name}
</div>
)}
</td>
</tr>);
return (rows);
}
renderRows=()=>{
const{receipts}=this.props
常量行=收据.map((收据,索引)=>(
{receipt.id}
{receipt.organization.name}
{receipt.receiptNumber}
{receipt.series}
{receipt.customer.fname}
{receipt.customer.lname}
{receipt.customer.patronymic}
{收据.客户.电话}
{receive.priceList.map((priceListEntry,index)=>
{priceListEntry.service.name}
)}
);
返回(行);
}
您可以执行const rows=receipts.reverse().map(/*[…]*/)
您还可以使用该函数创建所需的确切顺序。(此处:const rows=receipts.sort((a,b)=>{return b.id-a.id})。map(/*[…]*/)
)您可以基于收据使用
renderRows = () => {
const { receipts } = this.props
receipts.sort((a, b) => b.id - a.id);
const rows = receipts.map((receipt, index) => (
<tr key={index}>
<td>{receipt.id}</td>
<td >{receipt.organization.name}</td>
<td>{receipt.receiptNumber}</td>
<td>{receipt.series}</td>
<td>{receipt.customer.fname}</td>
<td>{receipt.customer.lname}</td>
<td>{receipt.customer.patronymic}</td>
<td>{receipt.customer.phone}</td>
<td>
{receipt.priceList.map((priceListEntry, index) =>
<div key={index}>
{priceListEntry.service.name}
</div>
)}
</td>
</tr>);
return (rows);
}
renderRows=()=>{
const{receipts}=this.props
收据.sort((a,b)=>b.id-a.id);
常量行=收据.map((收据,索引)=>(
{receipt.id}
{receipt.organization.name}
{receipt.receiptNumber}
{receipt.series}
{receipt.customer.fname}
{receipt.customer.lname}
{receipt.customer.patronymic}
{收据.客户.电话}
{receive.priceList.map((priceListEntry,index)=>
{priceListEntry.service.name}
)}
);
返回(行);
}
您可以使用javascript
排序
函数:
renderRows = () => {
let receipts = this.props.receipts
receipts.sort(function(a,b){ return b.id - a.id})
const rows = receipts.map((receipt, index) => (
<tr key={index}>
<td>{receipt.id}</td>
<td >{receipt.organization.name}</td>
<td>{receipt.receiptNumber}</td>
<td>{receipt.series}</td>
<td>{receipt.customer.fname}</td>
<td>{receipt.customer.lname}</td>
<td>{receipt.customer.patronymic}</td>
<td>{receipt.customer.phone}</td>
<td>
{receipt.priceList.map((priceListEntry, index) =>
<div key={index}>
{priceListEntry.service.name}
</div>
)}
</td>
</tr>);
return (rows);
}
renderRows=()=>{
让收据=this.props.receipts
receipts.sort(函数(a,b){return b.id-a.id})
常量行=收据.map((收据,索引)=>(
{receipt.id}
{receipt.organization.name}
{receipt.receiptNumber}
{receipt.series}
{receipt.customer.fname}
{receipt.customer.lname}
{receipt.customer.patronymic}
{收据.客户.电话}
{receive.priceList.map((priceListEntry,index)=>
{priceListEntry.service.name}
)}
);
返回(行);
}
尝试使用sort()
或reverse()
这意味着ID已经被排序,您不能确定这一点是的,我添加了排序函数来处理这个问题