简体   繁体   English

如何基于JS中的子值获取父JSON对象

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

My JSON is as follows: 我的JSON如下:

[ 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 ? 如何获得子元素具有nextTrueStepId = 3的父级(例如arr [parentID])?

I was using a forEach like this, but it iterates the rows sequentially: 我正在使用像这样的forEach,但是它依次迭代行:

 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? 编辑:杰森现在看起来像下面,但是当我调用arr [0]时,我只是返回“ [”而不是该行?

[
{
    "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: 修复JSON数据中的错误并将其存储到input ,假设您只希望匹配一个项目:

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

Code snippet (note it is ES6!): 代码段(请注意是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). 我制作了3种解决方案,也添加了ES5版本,将它们包装在可重用的函数中,然后还测试了执行速度(仅函数,在设置JS中声明函数不在基准范围内)。

Latest version, ES5 for loop is way fastest too. 最新版本,ES5 for loop也是最快的方式。 More code to write and less readable though. 虽然编写的代码更多,但可读性却更低。 Benchmark: https://jsbench.me/r8izihgj9w/2 基准: 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. 除了其他解决方案,您还可以使用Array#some早日返回函数,该函数用于在具有对象的数组内部查找特殊键和值。

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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将计算值推送到多维JSON对象并对父对象和子对象进行排序Vue.JS - How to Push Calculated Value to Multidimensional JSON Object and Sort Parent and Child Objects Vue.JS 如何使用Angular.js / Javascript通过使用子数组键值获取父对象值 - How to get parent object value by using child array key value using Angular.js/Javascript JavaScript:如何通过子对象值获取父对象键? - JavaScript: How to get parent object key by child object value? 如何使用子值获取父 object 值 - How to get the parent object value using child value 如何从javascript中的json对象获取所有父级和子级节点 - How to get all the parent and child node from a json object in javascript 如何从Vue js中另一个父级的子组件中获取值? - How to get value from the Child component of another Parent in Vue js? 如何在 nuxt.js 上从子组件获取值到父组件? - How to get an value from Child component to Parent on nuxt.js? 如何在js中创建父和子json树 - How to make a parent and child json tree in js 如何将父属性移动到子对象 JSON - How to Move Parent Property to Child Object JSON 如何根据同一个 JavaScript 父级中另一个子级的文本获取子级的值 - How to get value of a child based on the text of another child within the same JavaScript parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM