Average of properties excluding some
There is already a JavaScript function to aggregate values in an array: array.reduce(). You can use it like this:

const invalidValueForItem2 = -9999;

let result = {
    item1: { sum: 0, count: 0},
    item2: { sum: 0, count: 0},
};

result = array.reduce(function(acc, item) {
    acc.item1.sum += item.Item1;
    ++acc.item1.count;

    if (item.Item2 != invalidValueForItem2) {
        acc.item2.sum += item.Item2;
        ++acc.item2.count;
    }
}, result);


Using an object to hold your temporary data we may scale better when you will need more properties and/or if you will average all properties without any knowledge about them. Don't you need to average both Item1 and Item2? Just drop the outer object and keep { sum: 0, count: 0}:

let result = { sum: 0, count: 0};

result = array.reduce(function(acc, item) {
    if (item.Item2 != invalidValueForItem2) {
        acc.sum += item.Item2;
        ++acc.count;
    }
}, result);


Now you have your result you can calculate the average:

const average2 = result.item2.count &gt; 0 ? (result.item2.sum / result.item2.count) : NaN;


Do not forget to handle the corner case where there aren't items to sum, in this example when there aren't items the average is NaN (but you might want 0), if Infinity is appropriate then just make the division.



Few more notes about your code:

Indentation is incredibly important. Write just few hundred lines without proper indentation and you won't be able to understand what your code is doing without re-reading everything (and probably introducing bugs here and there).

Unless you have to support older browser you'd better use let instead of var (and const where appropriate).

Let your code breath, add space (both horizontally and vertically) and it will be much easier to read. To make code unreadable but small is the job of Minifiers/Uglifiers! Compare this with your original call to reduce():

var avg1 = array.reduce(function(sum, a, i, ar) {
    sum += a.item1; 
    return i == ar.length - 1 ? (ar.length == 0 ? 0 : sum / ar.length) : sum;
}, 0);


Those lines let me introduce few more things:

Do not nest ternary operators unless you're dealing with incredibly simple expressions.

You do not need all the parameters of the reduce() function (ar is just array) then it might be function(sum, a). Also you'd better find a meaningful name for a (especially in this case because function documentation talks about an accunulator then it may be misleading if you do not work often with it). Removing i will also stop you to do two things in this function (accumulate the sum and then return the average for the last one).

If your accumulator is a number (initialized to zero) then you do not need to explicitly give it as parameter.

How can you rewrite this? Just few lines when using an arrow function:

let sum = array.reduce((acc, item) =&gt; acc + item.item1);
let average = sum / array.length;


However you won't need this code because average for Item1 is calculated together with Item2, use this code for simpler cases.