2

Sorry I'm a new student, sorry this is so simple yet I still do not understand the object even after some research.

var obj = [
    {day:"monday", status:"present" , reason:""},
    {day:"tuesday", status:"present" , reason:""},
    {day:"wednesday", status:"absence" , reason:"sick"},

  ]

to be like this :

Total days: 3 
Total present: 2 
Total absence: 1 

do i have to use iteration ?

for (var key in obj) {
    var currentObj = obj[key];
}
var totaldays = obj.length ;
console.log(totaldays);

and why var totaldays = obj.length ; didnt work without iteration ?

also #2 question

{day:"monday", status:"present" , reason:""},

and

{"day":"monday", "status":"present" , "reason":""},

are those same or not ? sorry i feel like more understand with questioning in person/community rather than read in w3school or another cause I've tried over and over again and still get confused. but I always read first, asking is my last option

Dell Watson
  • 367
  • 1
  • 3
  • 13
  • That is Array of Object – RizkiDPrast Nov 16 '16 at 09:02
  • #1 `var totaldays = obj.length;` should work and for #2 read [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Satpal Nov 16 '16 at 09:02

5 Answers5

3

Q1, Count

You could use an object with the status as key and count up.

var obj = [{ day: "monday", status: "present", reason: "" }, { day: "tuesday", status: "present", reason: "" }, { day: "wednesday", status: "absence", reason: "sick" }],
    count= { days: 0 };

obj.forEach(function (o) {
    count[o.status] = (count[o.status] || 0) + 1;
    count.days++;
});
   
console.log(count);

Q2, object initializer

{ day: "monday", status: "present", reason: "" }

and

{ "day": "monday", "status": "present", "reason": ""}

are basically the same. If the identifier is like a valid variable name, you could omit the quotes.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

question 1:

var totaldays = obj.length ;

didn't work without iteration ?, Because i think the object obj is not get executed correctly, if the object is executed by the compiler correctly you will get the length.

Try this sample fiddler. https://jsfiddle.net/1sb9r9uc/

question 2:

Those are basically the same. If the identifier is like a valid variable name, you can eliminate the "".

Arun Raj R
  • 2,197
  • 3
  • 13
  • 23
0

i made a small fiddle

var obj = [
    {day:"monday", status:"present" , reason:""},
    {day:"tuesday", status:"present" , reason:""},
    {day:"wednesday", status:"absence" , reason:"sick"},

  ]
  var tdays=0;
  var pr=0;
  var ab=0;
  tdays=obj.length;
  for(var i=0;i<obj.length;i++)
  {
  if(obj[i].status=='present')
    pr++;

   if(obj[i].status=='absence')
   ab++;
  }

  console.log('Total days:'+tdays)
  console.log('Present: '+pr)
  console.log('Absence:'+ab)

This might be helpful https://jsfiddle.net/ubq4058s/

M14
  • 1,780
  • 2
  • 14
  • 31
0

Think obj.length can work well, don't know why it doesn't work for an array.

For the question#2, they should be same, I usually don't use string as object property name just like {a:100} not {"a":100}

The following code work pretty well and think it's easy to understand

var obj = [
    {day:"monday", status:"present" , reason:""},
    {day:"tuesday", status:"present" , reason:""},
    {day:"wednesday", status:"absence" , reason:"sick"}
  ];

var present=0,absence=0;

obj.forEach(function(v){
    if(v.status=="present") present+=1;
    if(v.status=="absence") absence+=1;
});

console.log("total days",obj.length);
console.log("total present",present);
console.log("total absence",absence);
Liang
  • 507
  • 2
  • 9
-1

You can count your items like that:

Object.keys(obj).length