値(または親の値)に基づいて論理条件を満たすJSON/JavaScript ArrayまたはObjectでアイテムを見つける方法を教えてください。複雑な条件を満たすフィルタ/検索オブジェクト/配列アイテム
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!
}
:私は私の
magicalWay
機能が
array.prototype.filter
どのようにいくつかを使用しなければならないと思います
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関数を定義する)私がしようとしている
追加:MySQLのJSON抽出と同様、 '$ [*]。items' retすべての項目のitems
の値を1つの配列に納めてください!
'true 'は' magicalWay() 'の2回目の呼び出しを意味します。達成したいことの詳細を追加する必要があります。 –
パスパターンの各項目について「true」を評価すると、それらはすべて受け入れ可能です。@AmreshVenugopal – MohaMad
私の理解によれば、2番目の引数に基づいて配列またはオブジェクト内の項目の値を求めますか? –