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

时间:2017-02-23 10:22:34

标签: javascript json

我的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 } ]

如何获取父元素(例如arr [parentID])where子元素的nextTrueStepId = 3?

我正在使用这样的forEach,但它按顺序迭代行:

 arr.forEach(function(row) {
      nextStep = processFlowRow(row, Id);
     });
编辑:Json现在看起来如下所示,但是当我调用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
}

4 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

修复JSON数据中的错误并将其存储到input后,假设您只希望匹配一个项目:

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

代码段(注意它是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))

答案 2 :(得分:0)

我已经制作了三种解决方案,添加了ES5版本,将它们包装在可重复使用的功能中,然后还测试了执行速度(仅限功能,声明功能超出基准,在安装JS中)。

最新版本,ES5 for loop也是最快的。编写更多代码,但可读性较差。基准: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))




答案 3 :(得分:0)

除了其他解决方案之外,您可以使用Array#some在函数中提前返回,以查看具有特殊键和值的对象的数组内部。

该函数返回与所需模式匹配的第一个对象。



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