日期:2014-05-16 浏览次数:20389 次
[{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}]
var arr = [{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}] ; for(var i=0;i<3;i++) { document.write(arr[i].A + '\n'); }
------解决方案--------------------
// 从数组中取出所有包含属性A的对象 var array = [{ "A": "a", "B": "b" }, { "A": "a", "B": "b" }, { "C": "c"}]; var result = $.grep(array, function (n, i) { for (var key in n) { if (key == "A") { return true; } continue; } }); //result = [{ "A": "a", "B": "b" }, { "A": "a", "B": "b" }]
------解决方案--------------------
Array.prototype.isHas=function (value){ var max=this.length,res=false; if(max==0){return false}; for(var i=0;i<max;i++){ for(var name in this[i]){ if(name==value){ res=true; break; } }; if(res){break} }; return res; }; var arr = [{ "A": "a", "B": "b" }, { "C": "a", "D": "b" }, { "e": "c"}]; var str=arr.isHas("A")?'存在':'不存在'; alert(str)
------解决方案--------------------
如果你的数组里面只是对象 比较容易
var arrTest = [{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}]; function isIncludeA(arrArr , sEqu){ if(arrArr instanceof Array){ var oObj = null; var arrResult = new Array(); for(var i = 0; i < arrArr.length; i++){ oObj = arrArr[i]; if(oObj instanceof Object){ for(var prop in oObj){ if(oObj[prop] == sEqu){ arrResult.push(oObj[prop]); } } } } return arrResult.length == 0 ? "没有匹配项" : ("匹配项个数:" + arrResult.length); } } window.onload = function(){ var result = isIncludeA(arrTest , "A"); alert(result); };
------解决方案--------------------
也没什么问题。只是写死了,结果返回不明确。思路是没有问题的
Array.prototype.isHas=function (a){ if(this.length==0){return false}; for(var i=0;i<this.length;i++){ if(this[i][a]){return true} } }; var arr = [{ "A": "a", "B": "b" }, { "C": "a", "D": "b" }, { "e": "c"}]; var str=arr.isHas("f")?'存在':'不存在'; alert(str);
------解决方案--------------------
判断对象数组里每个对象是否含有指定属性
var arr = [{"theforever_csdn":"a","B":"b"},{"theforever_csdn":"a","B":"b"},{"碧海情天":"c"}] ;
for(var i=0;i<arr.length;i++)