13

How would I count how many objects there are inside an array?

The array looking like:

[ {id: 1}, {id: 2}, ...]

I assume I could use count() if it was PHP, but what about NodeJS/Javascript?

Edit:

asd

if (offer.items_to_receive.length > 0) { 
    console.log("items: " + offer.items_to_receive.length);
    for(var i = 0; i < offer.items_to_receive.length; i++) {
        usersInRound.push(offer.steamid_other);
    }
}

logData('Accepted trade offer from ' + offer.steamid_other + '. (tradeofferid: ' + offer.tradeofferid + ')\nWorth ' + offer.items_to_receive.length + ' tickets. ');

How come it can read the "worth X tickets", but not the other part?

6
  • if your question was caused by your own typo (something you seem to indicate in a comment), then please delete your question. Commented Mar 17, 2015 at 6:02
  • Well it seems it wasn't, two seconds, updating the post. Commented Mar 17, 2015 at 6:04
  • Based on your edit, offer.items_to_receive is undefined. There is some problem earlier in your code that is making that property not be what you think it is. And, you certainly can't read the .length property of undefined. This should be basic debugging of your own code. Commented Mar 17, 2015 at 6:07
  • Well how come it can read it at the end? (Check the \nWorth X tickets part) it can read the .length there? Commented Mar 17, 2015 at 6:13
  • It can also read the .length of it at the "items" part. Commented Mar 17, 2015 at 6:15

4 Answers 4

28

Use .length

var data = [{
    id: 1
}, {
    id: 2
}];

console.log(data.length);

Update, in your edit I see that

offer.items_to_receive is undefined, ensure that object offer has property items_to_receive (should be array);

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

Comments

10
list = //YOUR OBJECT DATA

count= Object.keys(list).length;

Object.keys() gives the count of keys

That should give proper count

Comments

1

I think your question should be How to get Array Size in Javascript?

Answer: Use the length method.


Examples:

[1,2,3].length
// => 3

var a = { num: 1, obj: {}, arr: [{}, 3, "asd", {q: 3}, 1] }
a.arr.length
// => 5

Comments

0

a.arr.length

.length is not working while used in express it says deprecated

Error: express deprecated res.send(status): Use res.sendStatus(status) instead

2 Comments

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.