0

I want to find how many numbers are greater than 0.

let tempObj = [
{"id":1,"servPrice":0,"bedPrice":0},
{"id":2,"servPrice":3,"bedPrice":5},
{"id":3,"servPrice":9,"bedPrice":0}
]

If I write array as method parameter and which prop I want to learn, I guess it will work. For example : servPrice = 2, bedPrice = 1

oguzcan
  • 13
  • 2
  • `tempObj.filter(({ servPrice }) => servPrice > 0).length` and `tempObj.filter(({ bedPrice }) => bedPrice > 0).length`. If you want the property to be dynamic, given `property`: `tempObj.filter(({ [property]: prop }) => prop > 0).length`. If you have an array of properties, such as `const properties = [ "servPrice", "bedPrice" ];` use `const counts = tempObj.reduce((result, object) => { properties.forEach((property) => { result[property] ??= 0; if(object[property] > 0){ ++result[property]; } }); return result; }, {});`. – Sebastian Simon Dec 28 '22 at 07:56
  • Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212), and use the static and instance methods of [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Dec 28 '22 at 07:57

0 Answers0