Home > database >  Get N nodes of JSON response
Get N nodes of JSON response

Time:07-14

I have below JSON response. I want to get first N nodes of the response. Say if N = 2 I should get response where JobRequisitionID is R_062894 and R_011886

How can this be done using javascript

{
"Report_Entry": 
[
    {
        "JobRequisitionID": "R_062894",
        "JobReqEvergreen": "0",
        "JobRequisitionCompRange_Min": "0"                   
    },
    {
        "JobRequisitionID": "R_011886",
        "JobRequisitionCompRange_Min": "0",
        "JobCodeData": "2017 ~"
       
    },
    {
        "JobRequisitionID": "R_138792",
        "JobReqEvergreen": "1",
        "JobRequisitionCompRange_Min": "0"
    },
    {
        "JobRequisitionID": "R_027041",
        "JobRequisitionCompRange_Min": "0",
        "JobCodeData": "2021 ~",
        "HiringManagerName": "Pamala Leonard ~"
    },        
    {
        "PrimaryRecruiterEmail": "[email protected](1099919) ~",
        "JobRequisitionID": "R_086998",
        "Campus": "No",
        "JobReqEvergreen": "1",
        
    },

]
}

CodePudding user response:

object.Report_Entry.slice(0, 2)

CodePudding user response:

use Array.filetr to get all matching or Array.find to get the first one matching

var obj = {
"Report_Entry": 
[
    {
        "JobRequisitionID": "R_062894",
        "JobReqEvergreen": "0",
        "JobRequisitionCompRange_Min": "0"                   
    },
    {
        "JobRequisitionID": "R_011886",
        "JobRequisitionCompRange_Min": "0",
        "JobCodeData": "2017 ~"
       
    },
    {
        "JobRequisitionID": "R_138792",
        "JobReqEvergreen": "1",
        "JobRequisitionCompRange_Min": "0"
    },
    {
        "JobRequisitionID": "R_027041",
        "JobRequisitionCompRange_Min": "0",
        "JobCodeData": "2021 ~",
        "HiringManagerName": "Pamala Leonard ~"
    },        
    {
        "PrimaryRecruiterEmail": "[email protected](1099919) ~",
        "JobRequisitionID": "R_086998",
        "Campus": "No",
        "JobReqEvergreen": "1",
        
    },

]
}

var  JobRequisitionID = "R_062894"

ans1 = obj.Report_Entry.filter(v=>{
return v.JobRequisitionID == JobRequisitionID
})
console.log(ans1)
ans2 = obj.Report_Entry.find(v=>{
return v.JobRequisitionID == JobRequisitionID
})
console.log(ans2)

see fiddle

  • Related