Neal Jones Neal Jones - 4 months ago 5x
Javascript Question

Check to see if Array of Objects have >1 property values that !== undefined

I have an array of objects like this:

var array = [
{"name": "one", "value": .33},
{"name": "one", "value": .54},
{"name": "one", "value": undefined},

{"name": "two", "value": .3},
{"name": "two", "value": undefined},
{"name": "two", "value": undefined},

{"name": "three", "value": undefined},
{"name": "three", "value": undefined},
{"name": "three", "value": undefined},

And I need to be able to see if any unique name (one/two/three) has only one number among it's "value" properties. So, in this example the answer would be Yes because the value properties of "two" are: .3, undefined, undefined.

I do have a good way to get the unique "name" fields into an array:

function Names(a) {
var temp = {};
for (var i = 0; i < a.length; i++)
temp[a[i].name] = true;
var r = [];
for (var k in temp)
return r;

nameArray = Names(array);

But when I start to look to actually do a loop I start to get confused. Just writing it out I'd think it would be like:

var count = 0;
for(objects with name == i){
for(isNaN(value) == false){
if(count > 1){
return true;

Of course, that is psuedo code and probably not even the right direction. Any help is appreciated, thanks for reading!


You can create object with count of values that are numbers for each unique name and then check if count of any name is 1 with some and that will return true/false

var array = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one"},{"name":"two","value":0.3},{"name":"two"},{"name":"two"},{"name":"three"},{"name":"three"},{"name":"three"}], 
    obj = {}

array.forEach(function(e) {
  if(!obj[]) obj[] = 0;
  if(typeof e.value == 'number') obj[]+=1;

var result = Object.keys(obj).some(e => { return obj[e] == 1});