如何根据JSON/JavaScript Array或Object的值(或它们的父值)满足逻辑条件?筛选和搜索符合复杂条件的对象/数组项目
什么我想(定义magicalWay功能!):
myArray = [
{"type":"A","items":[0,1,2,3,4]},
{"type":"B","items":['x','y','z']}
];
magicalWay(myArray,"<parent>.type=='A'","<this>.items");
//and output: [0,1,2,3,4]
magicalWay(myArray,"true","<this>.items");
//and output: [[0,1,2,3,4],['x','y','z']]
myObject = {
"type": "A",
"items": [
{
"type": "B",
"items": ['x','y']
},
{
"type": "C",
"items": [0,1]
}
]
};
magicalWay(myObject,"true","<this>.items[*].items");
//and output: [['x','y'],[0,1]]
任何建议,帮助我:)
我觉得我magicalWay
功能必须使用array.prototype.filter
一些如何:
function magicalWay(myVar,strCondition,strPattern){
//replacing strCondition groups like [*] and others, and evaluate strCondition for each searching items.
//strPattern is which path should be extract and search
//and myVar is which searching through!
}
附加:就像MySQL JSON提取''[*]。items'ret将所有项目的items
值都放在一个数组中!
'true'对于'magicicalWay()'的第二次调用意味着什么。您需要添加更多关于您想要实现的细节。 –
评估路径模式中的每个项目的“真”意味着它们都可以接受@AmreshVenugopal – MohaMad
所以根据我的理解,您想要基于第二个参数的数组或对象内的项目的值? –