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 (e.g 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
}
]
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 (e.g 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
}
]
Share Improve this question edited Feb 23, 2017 at 11:47 Wayneio asked Feb 23, 2017 at 10:22 WayneioWayneio 3,5869 gold badges47 silver badges76 bronze badges 4- 2 Just to help with your searching: JSON is a textual notation for data exchange. (More) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. (If the above were in a string, it would be invalid JSON, as property names in JSON must be in double quotes.) What you have there is an array with objects, not JSON. – T.J. Crowder Commented Feb 23, 2017 at 10:23
- actually, it is not a valid data structure. – Nina Scholz Commented Feb 23, 2017 at 10:24
- To solve your problem, provide please console.log of source variable and example of result needed. – Максим Владимирович Commented Feb 23, 2017 at 10:25
- You are thinking about this wrongly. You aren't looking for a "parent object". You are looking for an object based in it's own property. – Turnip Commented Feb 23, 2017 at 10:25
3 Answers
Reset to default 1Try 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))