简体   繁体   English

我怎样才能 map 并返回反应中的第一个和最后一个元素?

[英]How can I map and return first and last element in react?

I am having problem with login return first element and last element:我在登录返回第一个元素和最后一个元素时遇到问题:

Here is my array of object:这是我的 object 数组:

0:
pointAmountMax: 99999
pointAmountMin: 1075
rateCode: ['INAINOW']
roomPoolCode: "ZZAO"
[[Prototype]]: Object
1:
pointAmountMax: 99999
pointAmountMin: 850
rateCode: ['INAINOJ']
roomPoolCode: "TOGA"
[[Prototype]]: Object
length: 2

How can I map through the array and return the pointAmountMin smaller number first?我怎样才能通过数组 map 并首先返回pointAmountMin较小的数字?

So in this case I want to achieve something like this in my front end所以在这种情况下,我想在我的前端实现这样的事情

return <p>850 - 1075</p>

To get the required output first you will need to sort the array of object in ascending order related to the property pointAmountMin.要首先获得所需的 output,您需要按照与属性 pointAmountMin 相关的升序对 object 的数组进行排序。

let arrayOfObj = [
    {
        pointAmountMax: 99999,
        pointAmountMin: 1075,
        rateCode: ['INAINOW'],
        roomPoolCode: "ZZAO",
    },
    {
        pointAmountMax: 99999,
        pointAmountMin: 850,
        rateCode: ['INAINOJ'],
        roomPoolCode: "TOGA",
    }
];

arrayOfObj.sort((a, b) => {
    return a.pointAmountMin - b.pointAmountMin
})
console.log(arrayOfObj)

Then collect only pointAmountMin values in an array.然后只收集数组中的 pointAmountMin 值。

const filteredResult = arrayOfObj.map(() => {
  return pointAmountMin
})

You have list of all pointAmountMin property values in ascending order (minimum first) Now you can show it on UI in your required format您拥有按升序排列的所有 pointAmountMin 属性值的列表(最小值在前)现在您可以在 UI 上以您需要的格式显示它

<p>filteredResult.join(' - ')</p>

Use sort() and then return the desired HTML使用sort()然后返回所需的 HTML

 function App(){ const myArray = [ { pointAmountMax: 99999, pointAmountMin: 1075, somethingElse: null, }, { pointAmountMax: 99999, pointAmountMin: 650, somethingElse: null, }, { pointAmountMax: 99999, pointAmountMin: 975, somethingElse: null, } ] const getRange = (data) => { let sorted = data.sort((a,b) => {a.pointAmountMin - b.pointAmountMin}) return [ <p key={Math.random()}> {sorted[0].pointAmountMin} - {sorted.pop().pointAmountMin} </p> ] } return <span>{getRange(myArray)}</span>; } ReactDOM.render(<App/>, document.getElementById("root"));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"/>

暂无
暂无

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

相关问题 我如何获得数组React js中的第一个唯一ID元素和最后一个唯一ID元素 - how can I get the first unique ID element and last unique ID element in an array React js 如何在 React Native sectionList 中找到第一项和最后一项? - How can I find the first and last item in a React Native sectionList? 如何为数组中的第一个元素返回属性值,如果没有元素则返回0? - How can I return a property value for the first element in an array or 0 if there are no elements? 在反应中,如何使用 map 向元素添加多个引用? - In react, how can I add multiple ref to the element using map? 我怎样才能在 map 中返回 map - how can i return map inside map 如何使用 DOM select html 文件的元素(第一个和最后一个除外)并对其进行操作? - How can I select the element of html file (except first and last) using DOM and manipulate it? 我如何从最后一个元素而不是第一个元素开始使用:nth-​​child()选择器? - How can I use the :nth-child() selector starting from the last element, rather than the first? 如何让 map function 返回一个大于第一个的数组? - How can I get the map function to return an array which is greater than the first? 如何使用Ramda.js映射对象值并仅采用第一个元素? - How can I map over object values using Ramda.js and take only first element? React Render Map最后一个元素不会更新 - React Render Map last element doesnt update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM