【发布时间】:2018-11-11 16:19:41
【问题描述】:
由于某种原因,我的数据排序方法在我的反应应用程序中不起作用,即返回值没有改变
将此视为我的状态和变量
state = {
percentageHighestOrLeast: "highest", //highest or lowest
townOrCounty: "town", //town or county
amountMaximumOrMinimum: "maximum" //maximum or minimum
}
现在在render 我正在检查数据是否在没有任何内容的情况下加载
错误然后调用函数
if (!this.props.mainListFetching && !this.props.mainListError) {
this.highestOrLeast = this.sortingData(this.props.mainList, this.state.percentageHighestOrLeast)
this.townOrCounty = this.sortingData(this.props.mainList, this.state.townOrCounty)
this.amountMaximumOrMinimum = this.sortingData(this.props.mainList, this.state.amountMaximumOrMinimum)
}
我的this.sortinfData 长这样(这个方法被成功调用了)
sortingData = (data, type) => {
data.sort((a, b) => {
if (type == "highest") return (a["percentage.funded"] - b["percentage.funded"])
if (type == "lowest") return (b["percentage.funded"] - a["percentage.funded"])
if (type == "town") return (a["type"].localeCompare(b["type"]))
if (type == "county") return (b["type"].localeCompare(a["type"]))
if (type == "maximum") return (a["amt.pledged"] - b["amt.pledged"])
if (type == "minimum") return (b["amt.pledged"] - a["amt.pledged"])
})
return data
}
如果我 console.log this.highestOrLeast 或 'this.amountMaximumOrMinimum' 或 this.townOrCounty,它们都会抛出相同的结果
这就是我的数据的样子
[
{
"s.no": 0,
"amt.pledged": 15823,
"blurb": "'Catalysts, Explorers & Secret Keepers: Women of Science Fiction' is a take-home exhibit & anthology by the Museum of Science Fiction.",
"by": "Museum of Science Fiction",
"country": "US",
"currency": "usd",
"end.time": "2016-11-01T23:59:00-04:00",
"location": "Washington, DC",
"percentage.funded": 186,
"num.backers": "219382",
"state": "DC",
"title": "Catalysts, Explorers & Secret Keepers: Women of SF",
"type": "Town",
"url": "/projects/1608905146/catalysts-explorers-and-secret-keepers-women-of-sf?ref=discovery"
},
{
"s.no": 1,
"amt.pledged": 6859,
"blurb": "A unique handmade picture book for kids & art lovers about a nervous monster who finds his courage with the help of a brave little girl",
"by": "Tyrone Wells & Broken Eagle, LLC",
"country": "US",
"currency": "usd",
"end.time": "2016-11-25T01:13:33-05:00",
"location": "Portland, OR",
"percentage.funded": 8,
"num.backers": "154926",
"state": "OR",
"title": "The Whatamagump (a hand-crafted story picture book)",
"type": "Town",
"url"
[问题:]谁能帮我弄清楚我在这里可能做错了什么?
【问题讨论】:
-
一个对象不能有一个没有像
amt.pledged那样引用的带有点的属性。数据的真实情况如何? -
@charlietfl sn-p of data is in question
-
但这根本是无效的,这是我的观点
-
@CertainPerformance 你的意思是这样的?
(a.percentage.funded - b.percentage.funded)这是抛出一个错误,提示 Cannot read property 'funded' of undefined -
提供实际有效数据的样本,而不是您从浏览器控制台复制的数据。拥有 runnable minimal reproducible example 可以防止猜测
标签: javascript reactjs