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

2

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

also add days in count object
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 "".

Comments

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/

Comments

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);

Comments

-1

You can count your items like that:

Object.keys(obj).length

1 Comment

This will not help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.