s-yadav / jsonQ

A JavaScript library to make manipulation and extraction of data from a JSON very easy and fast.
MIT License
202 stars 69 forks source link

Filtering returning an empty set #21

Open Basiczombie opened 5 years ago

Basiczombie commented 5 years ago

This may not be an issue with the project but more on how I'm formatting the data. I have search all over and can't find a solution other than to just ask directly.

So, the issue I'm having is that given a Json record that has been compiled through webpack with babel compilation and some manual process I can run .find(), which returns and Array with the teachers json, but can't filter what jsonQ is finding. Below is the code. There is no actual teacher or student data contained within. The Json was created for testing purposes only.

The goal is to have jsonQ to receive data that has been packaged like I have laid out and filter the teachers by grade_level so that I can obtain a list of names of teachers by that grade_level.

Any assistance on how to reformat the data to work with my project would be greatly appreciated.

import jsonQ from 'jsonq'
import students from '../public/json/students.json'
import teachers from '../public/json/teachers.json'

$(() => {
jsonQ.settings.sort.caseIgnore = false
let psData = {
    'students': students,
    'teachers': teachers
}

let data = jsonQ(psData)
let testFind = data.find('teachers')
console.log(testFind.value())
let testFilter = testFind.filter({ grade_level: 3 })
console.log(testFilter.value())
}

Json files that are imported through webpack.

Teachers:

[
    {
        "name": "Mrs. Marry",
        "grade_level": 4
    },
    {
        "name": "Ms. Booth",
        "grade_level": 5
    },
    {
        "name": "Mr. Kenneth",
        "grade_level": 6
    },
    {
        "name": "Mr. Smith",
        "grade_level": 0
    },
    {
        "name": "Mrs. Kemp",
        "grade_level": 1
    },
    {
        "name": "Ms. Jones",
        "grade_level": 2
    },
    {
        "name": "Mr. Dale",
        "grade_level": 3
    }
]

Students:

[
    {
        "firstlast": "Robinson, Billy",
        "dcid": 4321542,
        "grade_level": 0,
        "gender": "male"
    },
    {
        "firstlast": "Anderson, Tommy",
        "dcid": 1234567,
        "grade_level": 1,
        "gender": "male"
    },
    {
        "firstlast": "Jolly, Heather",
        "dcid": 2345678,
        "grade_level": 2,
        "gender": "female"
    },
    {
        "firstlast": "Gibbs, Abby",
        "dcid": 3456789,
        "grade_level": 3,
        "gender": "other"
    },
    {
        "firstlast": "Drake, John",
        "dcid": 4567890,
        "grade_level": 4,
        "gender": "male"
    },
    {
        "firstlast": "Ford, Zoey",
        "dcid": 2345234,
        "grade_level": 5,
        "gender": "female"
    },
    {
        "firstlast": "Peterson, Adrian",
        "dcid": 3245672,
        "grade_level": 6,
        "gender": "other"
    },
    {
        "firstlast": "Bell, Chris",
        "dcid": 6432723,
        "grade_level": 7,
        "gender": "female"
    }
]

Console .find() return with .value()

[Array(7)]
  0: Array(7)
    0: {name: "Mrs. Marry", grade_level: 4}
    1: {name: "Ms. Booth", grade_level: 5}
    2: {name: "Mr. Kenneth", grade_level: 6}
    3: {name: "Mr. Smith", grade_level: 0}
    4: {name: "Mrs. Kemp", grade_level: 1}
    5: {name: "Ms. Jones", grade_level: 2}
    6: {name: "Mr. Dale", grade_level: 3}
    length: 7
    __proto__: Array(0)
    length: 1
  __proto__: Array(0)