如何根據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
所以根據我的理解,您想要基於第二個參數的數組或對象內的項目的值? –