過去にこの種の状況に遭遇したときはいつでも、再帰検索機能は常に非常に貴重でした...これは私が横たわっていた私のものの1つです(removeメソッドを追加して拡張しました):
function o ( target ) {
/// if an instance of o has been created, then handle it
if ( this instanceof o ) {
/// O INSTANCE:
/// an instance of o acts like a reference or pointer
/// to how the value it targets was reached.
this.value = target;
this.key = arguments[1];
this.parent = arguments[2];
this.toString = function(){ return 'o('+this.key+' = '+this.value+')';}
}
/// if no instance being created fall back to the 'scan object' code
else {
/// RECURSIVE CODE:
/// the _ function is responsible for accepting the
/// attributeName and attributeValue search
var _ = function ( key, value, modifier ) {
var i, v, tar = ( modifier ? modifier.target : target ), items = [];
/// if we are dealing with an o instance, handle slightly differently
if ( tar instanceof o ) {
for ( i in tar.value ) {
/// check to see if our current key and value
/// match our search terms
if ( _.test( i, (v=tar.value[i]), key, value ) ) {
items.push(new o(v, i, tar));
};
};
}
/// if no o instance treat as a normal object or array
else {
for ( i in tar ) {
if ( (v = tar[i]) ) {
/// if we are an instance of o, recurse to actually
/// check the items within
if ( v instanceof o ) {
items = items.concat( _( key, value, {target:v} ) );
}
/// check to see if our current key and value match
/// our search terms
else if ( _.test( i, v, key, value ) ) {
items.push(new o(v, i, tar));
};
};
};
};
/// if in modifier mode, don't rely on storing in scope,
/// return the calculated items instead
if ( modifier ) {
return items;
}
else {
/// update what we are targeting
target = items;
/// return our underscore function
return _;
};
};
/// FUNCTION DECLARATIONS:
/// a quick test to see if the key and value match (or either or)
_.test = function ( i,v,key,value ) {
var havekey = ( key !== null && key !== undefined ),
haveval = ( value !== null && value !== undefined ),
passkey = ( havekey && (i == key || key === '*') ),
passval = ( haveval && (v == value || value === '*') );
return ( havekey && haveval && passkey && passval ) ||
( havekey && !haveval && passkey ) ||
( haveval && !havekey && passval );
};
/// calculate the path needed to reach the object within the structure
_.path = function () {
var i = target.length, paths = [], path, cur, last;
while ( i-- ) {
cur = target[i]; path = [];
do{ last = cur; if ( cur instanceof o ){ path.unshift( cur.key ); } }
while( (cur = cur.parent) );
paths.push(path.join('/'));
};
return ( paths.length == 1 ? paths[0] : paths );
};
/// remove the item we are targeting by stepping back
/// and deleting ourselves from the previous parent
_.remove = function ( removeEntireObject ) {
var i = target.length, paths, path, cur, last;
while ( i-- ) {
cur = target[i];
/// remove the object that has the found attribute
if ( removeEntireObject ) {
if ( cur.parent.parent ) {
cur.parent.parent.value[cur.parent.key] = null;
delete cur.parent.parent.value[cur.parent.key];
}
}
/// otherwise remove only the targeted attribute
else {
cur.parent.value[cur.key] = null;
delete cur.parent.value[cur.key];
}
};
return _;
};
/// a useful function for backwards navigation
_.parent = function () {
var i = target.length, cur, items = [], values = [];
while ( i-- ) {
cur = target[i];
/// remove the object that has the found attribute
if ( cur && cur.parent ) {
/// store the values as we go so we can
/// spot and remove duplicated parents
if ( values.indexOf(cur.parent.value) === -1 ) {
items.push(cur.parent);
values.push(cur.parent.value);
}
}
};
target = items;
return _;
}
/// slimple debugging
_.alert = function () {
var i = target.length, cur;
while ( i-- ) {
cur = target[i];
alert(cur);
};
return _;
};
return _;
};
};
使用例:
/// remove only the guestId object with a value '01'
o(tables)('*')('seats')('*')('guestId', '01').remove( true );
また:
/// remove the 'guestIds' object in the first slot for either seat
o(tables)('*')('seats')(0)('guestId', '*').parent().remove();
また:
/// remove all 'guestIds' from the first seat
o(tables)(0)('seats')('*')('guestId').parent().remove();
説明:
- 常に を呼び出すことから始める必要があります
o(my_object_to_parse)
。
- 1 つのパラメーターを渡すと、
attributeName
検索として機能します。
- 2 つのパラメーターを渡すと、
attributeName
andattributeValue
検索として機能します。
- は
*
、基本的な配列を処理するのに役立つ単純なワイルドカードとして機能します。
- はまたは
*
として使用できます。attributeName
attributeValue
- 連続する各要求は、構造の 1 レベル先に移動します。
このコードはしばらく使用していないため、100% バグがない、または最適ではない可能性があります。ただし、特定のユースケースではうまくいくようです...そして、テスト中に私が投げたすべてを処理することもできました。.remove()
、.parent()
、.path()
および以外のメソッドで拡張するのは簡単なはず.alert()
です。エラー チェックを追加するのが最善かもしれません。