[英]Checking the existence of an object with a certain property within an array
请记住,我还没有足够的词汇量,不知道要搜索什么关键字(现在就跳到编码浪潮上……),所以这可能是一个很容易回答的问题!
我说有很多物件
M = [A_1, A_2, ..., A_n]
,其中A_i是该类的实例化
A = {text: "string", coords:[x,y]}
我的问题是:检查我的数组M是否包含对象A_i的快速,简洁,简单的方法是什么?
A_i.text="your mom" && A_i.coords = [9,9]
是真的?
我可以访问underscore.js,这似乎有一些不错的处理方式,但是我对如何实现它有些困惑...
我尝试过的(使用underscore.js库)是:
var isitso = _.find(M, function (i) {
return i.text == "your mom" && i.coords == [9,9];
});
在学习JS的这一阶段,我将以多种不同的方式解决此问题。 只是看看可以做什么以及如何做。
提前致谢!
您所遇到的困难是数组相等是引用相等(它们必须在相同的内存位置),而不是值相等(它们具有相同的值)。 如果您将支票更改为:
return i.text == 'your mom' && i.coords[0] == 9 && i.coords[1] == 9;
它应该工作。 在这种情况下,您要检查第一个(0)和第二个(1)元素是否具有不比较数组对象本身的必需值。 比较数字确实比较值而不是参考。
您不能检查数组是否相等。 您需要执行以下操作:
_.find(M, function (i) {
return i.text == "your mom" && i.coords[0] == 9 && i.coords[1] == 9;
});
使用_.filter方法。
var filtered = _.filter(M, function(i) { return (i.text == "text2" && i.cords[0] == 3 && i.cords[1] == 4) });
从来没有听说过下划线js,所以我无法详细说明。 大多数Javacript框架都有一种或另一种类型的foreach方法,以模拟尚未标准化的foreach循环。
function findMatches(/* target array*/ target, /* matching function*/ match) {
var dummy = [];
dojo.forEach(target, function(t) {
if (match(t)) {
dummy.push(t);
}
});
return dummy;
}
var allMatches = findMatches(M, function (m) {
m.text == "your mom" && m.coords == [9,9]
});
请注意, dojo.forEach
是我使用的Dojo库中的forEach。 jQuery还包含您的首选库所没有的东西。
最近,我没有将此代码放入实际的浏览器中,但是即使我输入了错误,这个想法还是合理的。
到目前为止,所有其他答案都使用第三方js库。 以下是尝试直接使用js解决方案的尝试。 小提琴: http : //jsfiddle.net/rNfph/
var M = [
{text:'one', coord: [0,0]},
{text:'two', coord: [1,1]},
{text:'three', coord: [2,2]},
{text:'four', coord: [3,3]},
{text:'five', coord: [4,4]},
{text:'six', coord: [5,5]},
{text:'seven', coord: [6,6]},
{text:'eight', coord: [7,7]},
{text:'nine', coord: [8,8]},
{text:'ten', coord: [9,9]}
];
var search = function(str, x, y){
for(var i=0, len=M.length; i<len; i++){
if( M[i].text === str && M[i].coord[0] === x && M[i].coord[1] === y ){
return true;
}
}
return false;
}
console.log( search('three', 2, 2) );
console.log( search('three', 1, 2) );
现在,把它和一粒盐一起吃。 我不是复杂性方面的专家。 最好有更好的方法来执行此操作,因为此实现需要潜在地遍历所有值。
如果对此有一个限制,要求'text'的值必须唯一,那么可以不使用数组而是将其全部构造为一个对象(字典)并进行检查,而无需进行任何迭代。
看到这个小提琴: http : //jsfiddle.net/4mrsh/
var M = {
'one': [0,0],
'two': [1,1],
'three': [2,2],
'four': [3,3]
}
var search = function(key, x, y){
if( !M[key] || !(M[key][0] === x && M[key][1] === y) ) return false;
return true;
}
console.log( search('three',2,2) ); //true
console.log( search('three',1,2) ); //false
if (!Array.prototype.each) {
Array.prototype.each = function(callback) {
var i, l = this.length;
for (i = 0; i < l; i += 1) {
if (callback(i, this[i]) === true) return;
}
}
}
function anMHasYourMom(m) {
var found;
m.each(function(i, el) {
return found = (
el.text === "your mom"
&& el.coords[0] === 9
&& el.coords[1] === 9
);
});
return found;
}
var M = [A_1, A_2, ..., A_n];
if (anMHasYourMom(M)) {
window.alert('It has one!');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.