通过React获取JSON中最大文件数?

时间:2019-04-16 12:58:15

标签: reactjs

我有一系列数据,这些数据在一个数组(json文件)中包含一些对象,它将通过fetch()请求显示。在名为totalcom的json文件的每个对象中都有一个文件。我想返回totalcom的最大值,但返回NaN。如何在Reactjs中获得最大和最小数量?

class App extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        library: null,
        .
        .
        .
    };
}
componentDidMount() {
    fetch('/json.bc', {
            method: 'get',
        }, )
        .then(response => response.text())
        .then(text => {
            let Maindata = JSON.parse(text.replace(/\'/g, '"'))
            this.setState(state => ({
                ...state,
                data: Maindata
            }), () => {
                this.reorganiseLibrary()
            })
        }).catch(error => console.error(error))
}
reorganiseLibrary = () => {
    const {
        data
    } = this.state;
    let library = data;
    .
    .
    .
}

// handle render of library
renderLibrary = () => {
    const {
        library
    } = this.state;

    if (!library || (library && library.length === 0)) {
        return <div className = "nodata" > no result < /div>
    }
    return Math.max(...library.map(item => item.totalCom), library[0].totalCom)
}
render() {
    const {
        library
    } = this.state;
    return ( <
        div > {
            this.renderLibrary()
        } < /div>
    )
  }
}
ReactDOM.render( < App / > , document.getElementById('Result'))

2 个答案:

答案 0 :(得分:0)

let int1 = 100 var int2: Int? = nil // unnecessary castings: int2 = Int(int1) // nothing shown here, because of Int init: init(_ value: Int) int2 = int1 as? Int // Conditional cast from 'Int' to 'Int' always succeeds 在某些参数无法转换为Math.MaxMDN doc)时返回NaN

在应用Number之前尝试清理数组的值和/或将它们转换为Number

答案 1 :(得分:0)

    return Math.max(...library.map(item => item.totalCom), library[0].totalCom)

尝试调试此语句,此处必须有一些不能转换为数字的值