发布
社区首页 >问答首页 >如何从JSON对象数组中检索不同类型的数据并对其进行处理?

如何从JSON对象数组中检索不同类型的数据并对其进行处理?
EN

Stack Overflow用户
提问于 2019-04-12 02:47:07
回答 3查看 532关注 0票数 0

有一个给定的JSON对象数组:

代码语言:javascript
代码运行次数:0
复制
data: [{
    "name": "alice",
    "subject": "maths",
    "marks": "79"
},{
    "name": "bob",
    "subject": "maths",
    "marks": "36"
},{
    "name": "clare",
    "subject": "maths",
    "marks": "87"
},{
    "name": "dean",
    "subject": "maths",
    "marks": "50"
},{
    "name": "elon",
    "subject": "maths",
    "marks": "34"
},{
    "name": "fred",
    "subject": "maths",
    "marks": "99"
},{
    "name": "alice",
    "subject": "chemistry",
    "marks": "97"
},{
    "name": "bob",
    "subject": "chemistry",
    "marks": "80"
},{
    "name": "clare",
    "subject": "chemistry",
    "marks": "66"
},{
    "name": "dean",
    "subject": "chemistry",
    "marks": "83"
},{
    "name": "elon",
    "subject": "chemistry",
    "marks": "45"
},{
    "name": "fred",
    "subject": "chemistry",
    "marks": "32"
},{
    "name": "alice",
    "subject": "physics",
    "marks": "32"
},{
    "name": "bob",
    "subject": "physics",
    "marks": "29"
},{
    "name": "clare",
    "subject": "physics",
    "marks": "98"
},{
    "name": "dean",
    "subject": "physics",
    "marks": "56"
},{
    "name": "elon",
    "subject": "physics",
    "marks": "57"
},{
    "name": "fred",
    "subject": "physics",
    "marks": "62"
}]

我想得到每个科目的最高分,以及那个得分的学生的名字。

打印所有科目中取得>60分的学生总数。

全班学生的平均成绩是多少?

班上第一名的总分之和是多少?打印所有科目的名称和分数的总和。

等。

我试过最大的分数:

代码语言:javascript
代码运行次数:0
复制
 getMax = (arr, prop) => {
    var max;
    for (var i = 0; i < arr.length; i++) {
      if (!max || parseInt(arr[i][prop]) > parseInt(max[prop])) max = arr[i];
    }
    return max;
  };

  render() {
    return (
      <div className="App">
        <h1>
          {this.state.data.subject === "maths"
            ? getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
        <h1>
          {this.state.data.subject === "physics"
            ? getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
        <h1>
          {this.state.data.subject === "chemistry"
            ? getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
      </div>
    );
  }

错误:

第116行:“getMax”未定义为no-undef 第121行:“getMax”未定义为no-undef 第126行:“getMax”未定义为no-undef

除此之外,另一个问题的逻辑是什么?

关于codepen/jsfinddle上的工作代码的链接将是很棒的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-12 03:48:57

给您,只需单击Run code snippet

代码语言:javascript
代码运行次数:0
复制
const data=[{name:"alice",subject:"maths",marks:"79"},{name:"bob",subject:"maths",marks:"36"},{name:"clare",subject:"maths",marks:"87"},{name:"dean",subject:"maths",marks:"50"},{name:"elon",subject:"maths",marks:"34"},{name:"fred",subject:"maths",marks:"99"},{name:"alice",subject:"chemistry",marks:"97"},{name:"bob",subject:"chemistry",marks:"80"},{name:"clare",subject:"chemistry",marks:"66"},{name:"dean",subject:"chemistry",marks:"83"},{name:"elon",subject:"chemistry",marks:"45"},{name:"fred",subject:"chemistry",marks:"32"},{name:"alice",subject:"physics",marks:"32"},{name:"bob",subject:"physics",marks:"29"},{name:"clare",subject:"physics",marks:"98"},{name:"dean",subject:"physics",marks:"56"},{name:"elon",subject:"physics",marks:"57"},{name:"fred",subject:"physics",marks:"62"}];

const getMax = (arr, prop) => arr
  .map(v => ({...v, [prop]: parseInt(v[prop])}))
  .reduce((a, c) => c[prop] > a[prop] ? c : a);

const getMaxSubject = (arr, subject, prop) => 
  getMax(arr.filter(v => v.subject === subject), prop);
  
const aboveThreshold = (arr, threshold, prop) => arr
  .filter(v => parseInt(v[prop]) > threshold);
  
const average = (arr, prop) => {
  const total = arr.reduce((a, c) => a + parseInt(c[prop]), 0);
  return total / arr.length;
}

const scoreSum = (arr, prop) => arr
  .reduce((a, c) => {
    let s = a.find(v => v.name === c.name);
    if (s === undefined) {
      s = { name: c.name, total: 0 };
      a.push(s);
    }
    s.total += parseInt(c[prop]);
    return a;
  }, []);

const mathsMax = getMaxSubject(data, 'maths', 'marks');
const chemistryMax = getMaxSubject(data, 'chemistry', 'marks');
const physicsMax = getMaxSubject(data, 'physics', 'marks');
const above60 = aboveThreshold(data, 60, 'marks');
const sums = scoreSum(data, 'marks');
const maxScore = getMax(sums, 'total');

// maximum marks scored in each subject and name of the student that scored that
console.log('maths:', mathsMax.name, mathsMax.marks);
console.log('chemistry:', chemistryMax.name, chemistryMax.marks);
console.log('physics:', physicsMax.name, physicsMax.marks);

// total no of students getting >60 marks across all subjects
console.log('above 60:', above60.length);

// average mark scored by the class across all subjects
console.log('average:', average(data, 'marks'));

// sum of total marks scored by the topper in the class
console.log('max score:', maxScore.name, maxScore.total);

票数 1
EN

Stack Overflow用户

发布于 2019-04-12 03:00:11

要调用getMax方法,需要使用this.getMax()

就像这样:

代码语言:javascript
代码运行次数:0
复制
 getMax = (arr, prop) => {
    var max;
    for (var i = 0; i < arr.length; i++) {
      if (!max || parseInt(arr[i][prop]) > parseInt(max[prop])) max = arr[i];
    }
    return max;
  };

  render() {
    return (
      <div className="App">
        <h1>
          {this.state.data.subject === "maths"
            ? this.getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
        <h1>
          {this.state.data.subject === "physics"
            ? this.getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
        <h1>
          {this.state.data.subject === "chemistry"
            ? this.getMax(this.state.data, this.state.data.marks)
            : ""}
        </h1>
      </div>
    );
  }
票数 0
EN

Stack Overflow用户

发布于 2019-04-12 03:07:13

您没有初始化组件以使其工作,这是一个简单的示例,以阐明该概念:

代码语言:javascript
代码运行次数:0
复制
class App extends React.Component {

  constructor(props){
    super(props);
    this.state = {};
    this.getStringA = this.getStringA.bind(this);
    this.getStringB = this.getStringB.bind(this);
  }

  componentDidMount(){
    this.getStringA();
    this.getStringB();
  }

  getStringA(){
    let _state = this.state;
    _state['A'] = 'A';
    this.setState(_state);
  }

  getStringB(){
    let _state = this.state;
    _state['B'] = 'B';
    this.setState(_state);
  }

  render() {
    return (
      <div>
        Hello React..!<br/>
        {this.state.A}<br/>
        {this.state.B}
      </div>
    );
  }

}

React.render(<App />, document.getElementById('app'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55643769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档