使用JSON过滤数据

时间:2017-04-08 01:03:30

标签: json filter filtering

我在JSON中有这些数据。我想过滤它,我只会得到那些带有相应数据的数据对象" Integrative Programming"在csSubject中。

csDetailsForChart JSON对象

"[{"csName":"Quiz #1 - Create a simple socket Program","csSubject":"Integrative Programming","csScore":"18","csPoint":"20"},
{"csName":"Quiz #2 - Create a simple RMI application","csSubject":"Integrative Programming","csScore":"25","csPoint":"40"},
{"csName":"Hello World Quiz","csSubject":"Integrative Programming","csScore":"90","csPoint":"150"},
{"csName":"Create a program quiz 4","csSubject":"Integrative Programming","csScore":"45","csPoint":"100"},
{"csName":"Quiz 5","csSubject":"Math","csScore":"45","csPoint":"60"},
{"csName":"Quiz on SQL","csSubject":"Database Management Systems","csScore":"100","csPoint":"130"},
{"csName":"Quiz 2 ERD diagram making","csSubject":"Database Management Systems","csScore":"40","csPoint":"55"},
{"csName":"Linear Equations","csSubject":"Math","csScore":"21","csPoint":"35"}]"

HTML

<form>
    <p>Choose your subject to view your performance on that subject.</p>

    <select id="subselect">
        <option selected disabled hidden>Select Subject</option>
    </select>
        <button onclick = "loadChart()";>Submit</button>
</form>

的JavaScript

function loadChart() {
    var csDetailsForChart = JSON.parse(localStorage.getItem('classStanding'));
    var subjectFromSelectSub = document.getElementById('subselect').value;
    var dataPoints = [];

    for (key in as){
        var percentPassed = ((Number(as[key].csScore1)) / (Number(as[key].csPoint1))) * 100;
        dataPoints.push({y: percentPassed, label: as[key].csName1});
    }

    var chart = new CanvasJS.Chart("chartContainer",{
        title:{
            text: subjectFromSelectSub
        },
        animationEnabled: true,
        animationDuration: 2000,
        axisX:{
            title: "Class Standing Name"
        },
        axisY:{
            title: "Percentage You Passed"
        },
        data: [{  
            type: "bar",
            toolTipContent: "{y}%",
            name: "Percentage You Passed",
            dataPoints: dataPoints
        }]
    });
    chart.render();
}

我正在使用CanvasJS绘制我的数据。我只是想知道如何提取那些被过滤的数据。比如,制作另一个JSON对象来存储那些过滤后的数据。谢谢!

0 个答案:

没有答案