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
}

]

Wayneio
  • 3,466
  • 7
  • 42
  • 73
  • 2
    Just to help with your searching: JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247) 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 Feb 23 '17 at 10:23
  • actually, it is not a valid data structure. – Nina Scholz Feb 23 '17 at 10:24
  • To solve your problem, provide please console.log of source variable and example of result needed. – Максим Владимирович Feb 23 '17 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 Feb 23 '17 at 10:25

3 Answers3

1

Try this:

//Assuming your data is in a variable named jsonObj
jsonObj.filter(function(elem){
    return elem.nextTrueStepId===3;
})
Aman Jain
  • 655
  • 5
  • 17
1

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))
Mirko Vukušić
  • 2,091
  • 1
  • 10
  • 14
  • This doesn't work for me. Could you please share a working snippet for the same? – Aman Jain Feb 23 '17 at 12:15
  • I've updated answer with ES6 snippet and to your latest JSON structure – Mirko Vukušić Feb 23 '17 at 12:38
  • Thanks! Both answers work, I don't know which posted first? – Wayneio Feb 23 '17 at 13:22
  • @MirkoVukušić Can you tell me from where to start learning ES6? – Aman Jain Feb 23 '17 at 13:33
  • @Wayneio, I'm not hunting for points so it's on you :) But if you're referring to "filter" solution, please note that event if it works, solutions are not the same. Filter is ES5 which is advantage (if you can't use ES6). It also can return array (not a single element) because it finds ALL items, not just the first one like find does. This can be good or bad, depending on your case. If you only need one, then filter is also 86% slower (obviously because it doesnt stop search when first item is matched). benchmark: https://jsbench.me/r8izihgj9w/1 – Mirko Vukušić Feb 23 '17 at 14:31
  • @Wayneio, as for learning ES6, in case you already know enough about ES5, then I'd look up "what's new in ES6" like here: http://es6-features.org – Mirko Vukušić Feb 23 '17 at 14:33
0

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))
Mirko Vukušić
  • 2,091
  • 1
  • 10
  • 14