I have some JSON-code which has multiple objects in it:
我有一些JSON代码,其中包含多个对象:
[
{
"MNGR_NAME": "Mark",
"MGR_ID": "M44",
"EMP_ID": "1849"
},
{
"MNGR_NAME": "Steve",
"PROJ_ID": "88421",
"PROJ_NAME": "ABC",
"PROJ_ALLOC_NO": "49"
}
]
My JSON loop snippet is:
我的JSON循环片段是:
function ServiceSucceeded(result)
{
for(var x=0; x<result.length; x++)
{
}
}
Could you please let me know how to check there is no occurence of "MNGR_NAME" in the array. (It appears twice in my case.)
你能否告诉我如何检查阵列中是否没有“MNGR_NAME”。 (在我的案例中,它出现了两次。)
7 个解决方案
#1
28
You need to access the result
object on iteration.
您需要在迭代时访问结果对象。
for (var key in result)
{
if (result.hasOwnProperty(key))
{
// here you have access to
var MNGR_NAME = result[key].MNGR_NAME;
var MGR_ID = result[key].MGR_ID;
}
}
#2
5
You could use jQuery's $.each:
你可以使用jQuery的$ .each:
var exists = false;
$.each(arr, function(index, obj){
if(typeof(obj.MNGR_NAME) !== 'undefined'){
exists = true;
return false;
}
});
alert('Does a manager exists? ' + exists);
Returning false
will break the each, so when one manager is encountered, the iteration will stop.
返回false会破坏每个,所以当遇到一个管理器时,迭代将停止。
#3
2
Note that your object is an array of JavaScript objects. Could you use something like this?
请注意,您的对象是一个JavaScript对象数组。你能用这样的东西吗?
var array = [{
"MNGR_NAME": "Mark",
"MGR_ID": "M44",
"EMP_ID": "1849"
},
{
"MNGR_NAME": "Steve",
"PROJ_ID": "88421",
"PROJ_NAME": "ABC",
"PROJ_ALLOC_NO": "49"
}];
var numberOfMngrName = 0;
for(var i=0;i<array.length;i++){
if(array[i].MNGR_NAME != null){
numberOfMngrName++;
}
}
console.log(numberOfMngrName);
#4
0
This will find the number of occurrences of the MNGR_NAME
key in your Object
Array
:
这将在对象数组中找到MNGR_NAME键的出现次数:
var numMngrName = 0;
$.each(json, function () {
// 'this' is the Object you are iterating over
if (this.MNGR_NAME !== undefined) {
numMngrName++;
}
});
#5
0
Within the loop result[x]
is the object, so if you wanted to count a member that may or may not be present;
在循环结果[x]中是对象,所以如果你想计算一个可能存在或不存在的成员;
function ServiceSucceeded(result)
{
var managers = 0
for(var x=0; x<result.length; x++)
{
if (typeof result[x].MNGR_NAME !== "undefined")
managers++;
}
alert(managers);
}
#6
0
You can iterate over the collection and check each object if it contains the property:
您可以迭代集合并检查每个对象是否包含属性:
var count = 0;
var i;
for(i = 0; i < jsonObj.length; i += 1) {
if(jsonObj[i]["MNGR_NAME"]) {
count++;
}
}
Working example: http://jsfiddle.net/j3fbQ/
工作示例:http://jsfiddle.net/j3fbQ/
#7
0
You could use $.each or $.grep, if you also want to get the elements that contain the attribute.
如果您还想获取包含该属性的元素,则可以使用$ .each或$ .grep。
filtered = $.grep(result, function(value) {
return (value["MNGR_NAME"] !== undefined)
});
count = filtered.length
#1
28
You need to access the result
object on iteration.
您需要在迭代时访问结果对象。
for (var key in result)
{
if (result.hasOwnProperty(key))
{
// here you have access to
var MNGR_NAME = result[key].MNGR_NAME;
var MGR_ID = result[key].MGR_ID;
}
}
#2
5
You could use jQuery's $.each:
你可以使用jQuery的$ .each:
var exists = false;
$.each(arr, function(index, obj){
if(typeof(obj.MNGR_NAME) !== 'undefined'){
exists = true;
return false;
}
});
alert('Does a manager exists? ' + exists);
Returning false
will break the each, so when one manager is encountered, the iteration will stop.
返回false会破坏每个,所以当遇到一个管理器时,迭代将停止。
#3
2
Note that your object is an array of JavaScript objects. Could you use something like this?
请注意,您的对象是一个JavaScript对象数组。你能用这样的东西吗?
var array = [{
"MNGR_NAME": "Mark",
"MGR_ID": "M44",
"EMP_ID": "1849"
},
{
"MNGR_NAME": "Steve",
"PROJ_ID": "88421",
"PROJ_NAME": "ABC",
"PROJ_ALLOC_NO": "49"
}];
var numberOfMngrName = 0;
for(var i=0;i<array.length;i++){
if(array[i].MNGR_NAME != null){
numberOfMngrName++;
}
}
console.log(numberOfMngrName);
#4
0
This will find the number of occurrences of the MNGR_NAME
key in your Object
Array
:
这将在对象数组中找到MNGR_NAME键的出现次数:
var numMngrName = 0;
$.each(json, function () {
// 'this' is the Object you are iterating over
if (this.MNGR_NAME !== undefined) {
numMngrName++;
}
});
#5
0
Within the loop result[x]
is the object, so if you wanted to count a member that may or may not be present;
在循环结果[x]中是对象,所以如果你想计算一个可能存在或不存在的成员;
function ServiceSucceeded(result)
{
var managers = 0
for(var x=0; x<result.length; x++)
{
if (typeof result[x].MNGR_NAME !== "undefined")
managers++;
}
alert(managers);
}
#6
0
You can iterate over the collection and check each object if it contains the property:
您可以迭代集合并检查每个对象是否包含属性:
var count = 0;
var i;
for(i = 0; i < jsonObj.length; i += 1) {
if(jsonObj[i]["MNGR_NAME"]) {
count++;
}
}
Working example: http://jsfiddle.net/j3fbQ/
工作示例:http://jsfiddle.net/j3fbQ/
#7
0
You could use $.each or $.grep, if you also want to get the elements that contain the attribute.
如果您还想获取包含该属性的元素,则可以使用$ .each或$ .grep。
filtered = $.grep(result, function(value) {
return (value["MNGR_NAME"] !== undefined)
});
count = filtered.length