MooTools - Object.contains 带通配符
MooTools - Object.contains with wildcard
我想在对象中搜索值。例如:
我正在搜索 "boy"。在对象中有一个值 "boy1" 但 Object.contains 不 return 为真。仅当我实际搜索 "boy1".
时
是否可以使用通配符或者是否有其他功能?
if(Object.contains(object, 'boy')) {
console.log("FOUND!");
}
我自己找到了答案:
Object.each(arrayWithObjects, function(object, index) {
Object.each(object, function(value, key) {
if(value.indexOf(searchTerm) > -1) {
foundContacts.push(object);
}
});
});
您可以使用 var foundContacts = arrayWithObjects.filter(...
和 Object.some()
来做到这一点:
Object.some():
Returns true if at least one value in the object satisfies the provided testing function.
var foundContacts = arrayWithObjects.filter(function(object){
return Object.some(object, function(value, key) {
return value.indexOf(searchTerm) > -1;
});
});
这样您就可以映射初始数组,并且不必遍历对象的所有键,因为 Object.some returns 及早找到匹配项。
我会考虑扩展 Array
,而不是使用类似于 lodash/underscore 中的 where
的内容。
var a = [{
name: 'bob',
age: 31,
colour: 'red'
}, {
name: 'john',
age: 38,
colour: 'blue',
job: 'chef'
}];
Array.implement({
where: function(match){
return Array.filter(this, function(el, index){
var allMatch = false, k;
for (k in match){
if (el.hasOwnProperty(k)){
allMatch = (typeof match[k] === 'function') ?
match[k].call(el, el[k], k, index) :
el[k] == match[k];
}
else {
// missing key we needed
allMatch = false;
}
}
return allMatch;
});
}
});
// call from native, match by string / value.
var r = Array.where(a, { name: 'bob' });
console.log('native, value match string', r);
r = Array.where(a, { age: 31 });
console.log('native, value match number', r);
// call from prototype, match by function callback
r = a.where({
name: function startsWith(value, key){
return value.indexOf('joh') === 0;
}
});
console.log('proto, single fn', r);
// multiple matches combos
r = a.where({
job: function hasAJob(job){
return !!job.length;
},
age: function(age){
return age > 35;
},
colour: 'red'
});
console.log('multiple matches combos', r);
这提供了一种足够灵活的方式来迭代您的对象集合。
在行动:http://jsfiddle.net/c8gaxkny/
看看我很久以前做过的这个有趣的事情 - 允许您使用类似于 CSS 的表达式来查找数组中的对象:
http://epitome-mvc.github.io/Epitome/#epitomecollection/find
代码位于 https://github.com/epitome-mvc/Epitome/blob/master/src/epitome-collection.js#L220
我想在对象中搜索值。例如: 我正在搜索 "boy"。在对象中有一个值 "boy1" 但 Object.contains 不 return 为真。仅当我实际搜索 "boy1".
时是否可以使用通配符或者是否有其他功能?
if(Object.contains(object, 'boy')) {
console.log("FOUND!");
}
我自己找到了答案:
Object.each(arrayWithObjects, function(object, index) {
Object.each(object, function(value, key) {
if(value.indexOf(searchTerm) > -1) {
foundContacts.push(object);
}
});
});
您可以使用 var foundContacts = arrayWithObjects.filter(...
和 Object.some()
来做到这一点:
Object.some():
Returns true if at least one value in the object satisfies the provided testing function.
var foundContacts = arrayWithObjects.filter(function(object){
return Object.some(object, function(value, key) {
return value.indexOf(searchTerm) > -1;
});
});
这样您就可以映射初始数组,并且不必遍历对象的所有键,因为 Object.some returns 及早找到匹配项。
我会考虑扩展 Array
,而不是使用类似于 lodash/underscore 中的 where
的内容。
var a = [{
name: 'bob',
age: 31,
colour: 'red'
}, {
name: 'john',
age: 38,
colour: 'blue',
job: 'chef'
}];
Array.implement({
where: function(match){
return Array.filter(this, function(el, index){
var allMatch = false, k;
for (k in match){
if (el.hasOwnProperty(k)){
allMatch = (typeof match[k] === 'function') ?
match[k].call(el, el[k], k, index) :
el[k] == match[k];
}
else {
// missing key we needed
allMatch = false;
}
}
return allMatch;
});
}
});
// call from native, match by string / value.
var r = Array.where(a, { name: 'bob' });
console.log('native, value match string', r);
r = Array.where(a, { age: 31 });
console.log('native, value match number', r);
// call from prototype, match by function callback
r = a.where({
name: function startsWith(value, key){
return value.indexOf('joh') === 0;
}
});
console.log('proto, single fn', r);
// multiple matches combos
r = a.where({
job: function hasAJob(job){
return !!job.length;
},
age: function(age){
return age > 35;
},
colour: 'red'
});
console.log('multiple matches combos', r);
这提供了一种足够灵活的方式来迭代您的对象集合。
在行动:http://jsfiddle.net/c8gaxkny/
看看我很久以前做过的这个有趣的事情 - 允许您使用类似于 CSS 的表达式来查找数组中的对象:
http://epitome-mvc.github.io/Epitome/#epitomecollection/find
代码位于 https://github.com/epitome-mvc/Epitome/blob/master/src/epitome-collection.js#L220