javascript - Check to see if Array of Objects have >1 property values that !== undefined -
i have array of objects 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 need able see if unique name (one/two/three) has 1 number among it's "value" properties. so, in example answer yes because value properties of "two" are: .3, undefined, undefined.
i have way unique "name" fields array:
function names(a) { var temp = {}; (var = 0; < a.length; i++) temp[a[i].name] = true; var r = []; (var k in temp) r.push(k); return r; } namearray = names(array);
but when start loop start confused. writing out i'd think like:
var count = 0; (objects name == i){ if (isnan(value) == false){ count++ if(count > 1) { return true; } } }
of course, pseudo code , not right direction. appreciated, reading!
you can create object count of values numbers each unique name , check if count of name 1 some
, 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[e.name]) obj[e.name] = 0; if(typeof e.value == 'number') obj[e.name]+=1; }); var result = object.keys(obj).some(e => { return obj[e] == 1}); console.log(result)