最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript count objects in array of objects without null values - Stack Overflow

programmeradmin5浏览0评论

Let's say we have array of objects:

const data = [
  {
    firstName: "Bruce",
    lastName: "Wayne",
    address: "Arkham 6",
    id: "1",
  },
  {
    firstName: "Peter",
    lastName: "Parker",
     address: "LA 54",
    id: "2"
  },
  {
    firstName: "Tony",
    lastName: "Stark",
    address: null,
    id: "3"
  }
];

and want to get length of the array but exclude counting of the objects which has null values (in example above, this is the last object with address property null) so that result of the counting of the example above would be 2.

objectsWithoutNull = data.reduce(function (r, a) {
    return r + +( a!== null);
}, 0);

I'm trying with reduce method, but got 0. Where is the problem in iteration?

Let's say we have array of objects:

const data = [
  {
    firstName: "Bruce",
    lastName: "Wayne",
    address: "Arkham 6",
    id: "1",
  },
  {
    firstName: "Peter",
    lastName: "Parker",
     address: "LA 54",
    id: "2"
  },
  {
    firstName: "Tony",
    lastName: "Stark",
    address: null,
    id: "3"
  }
];

and want to get length of the array but exclude counting of the objects which has null values (in example above, this is the last object with address property null) so that result of the counting of the example above would be 2.

objectsWithoutNull = data.reduce(function (r, a) {
    return r + +( a!== null);
}, 0);

I'm trying with reduce method, but got 0. Where is the problem in iteration?

Share Improve this question edited May 22, 2019 at 10:46 rrk 15.9k4 gold badges30 silver badges47 bronze badges asked May 22, 2019 at 10:46 corrycorry 1,5297 gold badges33 silver badges64 bronze badges 2
  • 2 do you want to look at a specific property or at all properties? – Nina Scholz Commented May 22, 2019 at 10:48
  • change to a.address!== null – Ravikumar Rajendran Commented May 22, 2019 at 10:52
Add a ment  | 

6 Answers 6

Reset to default 2

You can filter() the array and than get object values and check where not includes null.

const data = [
  {
    firstName: "Bruce",
    lastName: "Wayne",
    address: "Arkham 6",
    id: "1",
  },
  {
    firstName: "Peter",
    lastName: "Parker",
     address: "LA 54",
    id: "2"
  },
  {
    firstName: "Tony",
    lastName: "Stark",
    address: null,
    id: "3"
  }
];

const notNullable = data.filter(obj=>!Object.values(obj).includes(null));
console.log(notNullable)
console.log(notNullable.length)

More about Object Values Filter Includes

Reduce the array, for each object get the values with Object.values(), and check with Array.includes() if it contains a null value. Negate with ! the boolean result of inclues, and use the + operator to convert to number. Add the number to the accumulator.

const data = [{"firstName":"Bruce","lastName":"Wayne","address":"Arkham 6","id":"1"},{"firstName":"Peter","lastName":"Parker","address":"LA 54","id":"2"},{"firstName":"Tony","lastName":"Stark","address":null,"id":"3"}];

const result = data.reduce((r, o) => 
  r + +!Object.values(o).includes(null)
, 0);

console.log(result);

An option is to iterate over the object-values of a and test if the value is equal to null. If we found a null don't increase our counter.

const data = [
  {
    firstName: "Bruce",
    lastName: "Wayne",
    address: "Arkham 6",
    id: "1",
  },
  {
    firstName: "Peter",
    lastName: "Parker",
     address: "LA 54",
    id: "2"
  },
  {
    firstName: "Tony",
    lastName: "Stark",
    address: null,
    id: "3"
  }
];

let objectsWithoutNull = data.reduce(function (r, a) {
  let hasNull = false;
  const values = Object.values(a);
  values.map(v => {
    if (v === null) {
      hasNull = true;
    }
  });
  
  return r + !hasNull;
}, 0);

console.log(objectsWithoutNull);

  objectsWithoutNull = data.reduce(function (r, a) {
        return r + +( Object.values(a).indexOf(null) == -1);
    }, 0); 

This will work if you want to check for all the properties!

You can use Array.prototype.every to check that all the values are not null

var length = data.filter(i => Object.values(i).every(i => i !== null)).length

If you are using lodash.

const data = [
      {
        firstName: "Bruce",
        lastName: "Wayne",
        address: "Arkham 6",
        id: "1",
      },
      {
        firstName: "Peter",
        lastName: "Parker",
         address: "LA 54",
        id: "2"
      },
      {
        firstName: "Tony",
        lastName: "Stark",
        address: null,
        id: "3"
      }
    ];

const counter = _.filter(data, ({address}) => {return address != null}).length;
console.log(counter);
发布评论

评论列表(0)

  1. 暂无评论