Reputation: 367
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
Upvotes: 2
Views: 581
Reputation: 386604
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);
{ 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.
Upvotes: 3
Reputation: 2317
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/
Those are basically the same. If the identifier is like a valid variable name, you can eliminate the "".
Upvotes: 1
Reputation: 515
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);
Upvotes: 0
Reputation: 1810
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/
Upvotes: 0