简体   繁体   中英

How to get parent JSON object based on child value in JS

My JSON is as follows:

[ RowDataPacket {
    workflowId: 1,
    stepId: 1,
    workflowTypeId: 4,
    baseFieldId: 3,
    relatedFieldId: 0,
    relatedValue: 'YES',
    nextTrueStepId: 2,
    nextFalseStepId: 4 },
  RowDataPacket {
    workflowId: 1,
    stepId: 2,
    workflowTypeId: 2,
    baseFieldId: 4,
    relatedFieldId: 0,
    relatedValue: '',
    nextTrueStepId: 3,
    nextFalseStepId: 4 },
  RowDataPacket {
    workflowId: 1,
    stepId: 3,
    workflowTypeId: 9,
    baseFieldId: 5,
    relatedFieldId: 0,
    relatedValue: 'SUBMITTED',
    nextTrueStepId: 4,
    nextFalseStepId: 0 },
  RowDataPacket {
    workflowId: 1,
    stepId: 4,
    workflowTypeId: 10,
    baseFieldId: 0,
    relatedFieldId: 0,
    relatedValue: '',
    nextTrueStepId: 0,
    nextFalseStepId: 0 } ]

How can I get the parent (eg arr[parentID]) where child element has a nextTrueStepId = 3 ?

I was using a forEach like this, but it iterates the rows sequentially:

 arr.forEach(function(row) {
      nextStep = processFlowRow(row, Id);
     });

EDIT: Json now looks like the below, but when I call arr[0] I just get back "[" instead of the row?

[
{
    "workflowId": 1,
    "stepId": 1,
    "workflowTypeId": 4,
    "baseFieldId": 3,
    "relatedFieldId": 0,
    "relatedValue": "yes",
    "nextTrueStepId": 2,
    "nextFalseStepId": 4
},
{
    "workflowId": 1,
    "stepId": 2,
    "workflowTypeId": 2,
    "baseFieldId": 4,
    "relatedFieldId": 0,
    "relatedValue": "",
    "nextTrueStepId": 3,
    "nextFalseStepId": 4
},
{
    "workflowId": 1,
    "stepId": 3,
    "workflowTypeId": 9,
    "baseFieldId": 1,
    "relatedFieldId": 0,
    "relatedValue": "SUBMITTED",
    "nextTrueStepId": 4,
    "nextFalseStepId": 0
}

]

Try this:

//Assuming your data is in a variable named jsonObj
jsonObj.filter(function(elem){
    return elem.nextTrueStepId===3;
})

After fixing errors in your JSON data and store it to input , assuming you expect only ONE item to match:

input.find(item=>item.nextTrueStepId === 3)

Code snippet (note it is ES6!):

 var input = [{ "workflowId": 1, "stepId": 1, "workflowTypeId": 4, "baseFieldId": 3, "relatedFieldId": 0, "relatedValue": "yes", "nextTrueStepId": 2, "nextFalseStepId": 4 }, { "workflowId": 1, "stepId": 2, "workflowTypeId": 2, "baseFieldId": 4, "relatedFieldId": 0, "relatedValue": "", "nextTrueStepId": 3, "nextFalseStepId": 4 }, { "workflowId": 1, "stepId": 3, "workflowTypeId": 9, "baseFieldId": 1, "relatedFieldId": 0, "relatedValue": "SUBMITTED", "nextTrueStepId": 4, "nextFalseStepId": 0 }] console.log(input.find(item=>item.nextTrueStepId === 3)) 

I've made 3 variatios of solution, added ES5 version too, wrapped them in reusable functions and then also tested execution speed (function only, declaring of function is outside of benchmark, in setup JS).

Latest version, ES5 for loop is way fastest too. More code to write and less readable though. Benchmark: https://jsbench.me/r8izihgj9w/2

 var input = [{ "workflowId": 1, "stepId": 1, "workflowTypeId": 4, "baseFieldId": 3, "relatedFieldId": 0, "relatedValue": "yes", "nextTrueStepId": 2, "nextFalseStepId": 4 }, { "workflowId": 1, "stepId": 2, "workflowTypeId": 2, "baseFieldId": 4, "relatedFieldId": 0, "relatedValue": "", "nextTrueStepId": 3, "nextFalseStepId": 4 }, { "workflowId": 1, "stepId": 3, "workflowTypeId": 9, "baseFieldId": 1, "relatedFieldId": 0, "relatedValue": "SUBMITTED", "nextTrueStepId": 4, "nextFalseStepId": 0 }] function findItem3(x) { for (i=input.length-1; i>=0; i--) { if (input[i].nextTrueStepId === 3) return input[i] } return {} } function findItem2(x) { return input.find(item => item.nextTrueStepId === x) } function findItem1(x) { return input.filter(function(elem){ return elem.nextTrueStepId===x; }) } console.log(findItem1(3)) console.log(findItem2(3)) console.log(findItem3(3)) 

Beside the other solutions, you could use Array#some for an early return in a function for looking inside of an array with objects for a special key and value.

The function returns the first object which matches the wanted pattern.

 function getObject(array, key, value) { var object; array.some(function (o) { if (o[key] === value) { object = o; return true; } }); return object; } var data = [{ workflowId: 1, stepId: 1, workflowTypeId: 4, baseFieldId: 3, relatedFieldId: 0, relatedValue: "yes", nextTrueStepId: 2, nextFalseStepId: 4 }, { workflowId: 1, stepId: 2, workflowTypeId: 2, baseFieldId: 4, relatedFieldId: 0, relatedValue: 0, nextTrueStepId: 3, nextFalseStepId: 4 }, { workflowId: 1, stepId: 3, workflowTypeId: 9, baseFieldId: 1, relatedFieldId: 0, relatedValue: "SUBMITTED", nextTrueStepId: 4, nextFalseStepId: 0 }]; console.log(getObject(data, 'nextTrueStepId', 3)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM