user4883426
user4883426

Reputation:

how can i access properties of an object

i have an array of objects and i want to return the 'message' property of each of the objects.

i got the objects by calling my own oModel.oData which i created. now that i have these 5 objects how can i get the "message" property from these 5?

also, is there a way to count the number of objects i have in total? e.g sum of oModel.oData objects?

*note the objects are inside an array.

enter image description here

Thank you in advance :)

Upvotes: 0

Views: 66

Answers (3)

Jimmy.B
Jimmy.B

Reputation: 225

You can use .length to count your objects inside an array.

 YourArray.length
this will return the numbers of element (in your case the object) inside the array.

As for the message you will need to loop each object inside of your array. you can easily do that using JQuery library https://jquery.com/

It will look like this

$(function(){
  $.each(YourArray,function(i){
    console.log(YourArray[i].message);
  });
});

Or in javascript

for (var i = 0; i < YourArray.length; i++) {
  console.log(YourArray[i].details);
};

Upvotes: 1

Thomas Sebastian
Thomas Sebastian

Reputation: 1612

Method 1:

for (var i = 0; i < oModel.oData.length; i++) {
  console.log(oModel.oData[i].message);
}

Method 2:

(oModel.oData).forEach(function (obj) {
  console.log(obj.message);
});

to get your objects length:

var _len = oModel.oData.length;
console.log(_len);  

You can read more about arrays here.

Upvotes: 0

FarazPatankar
FarazPatankar

Reputation: 11

If it's an array, you can simply do array.length to get the total number of objects.

As for getting the message from each of them, you'd simply have to loop over the array like :

array.forEach(function(obj) { console.log(obj.message) });

Let me know if you have any more questions.

Upvotes: 0

Related Questions