Check if the array of objects> 1 has property values ​​that == == w90>

I have an array of such objects:

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 of its properties β€œvalue”. Thus, in this example, the answer is Yes, because the property values ​​are two:. 3, undefined, undefined.

I have a good way to get 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)
    r.push(k);
  return r;
}

nameArray = Names(array);

But when I start looking at the cycle, I start to get confused. Just by writing this, I would have thought it would be like this:

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

Of course, this is pseudo-code, and perhaps not even the right direction. Any help is appreciated, thanks for reading!

+4
source share
3

, , , 1 some 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)
Hide result
+3

, , Nenad.

, :

//Should be false
var array1 = [{"name":"one","value":0.33}];
//Should be false
var array2 = [{"name":"one","value":0.33},{"name":"one","value":0.54}];
//Should be false
var array3 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one","value":undefined}];
//Should be true - middle sequence contains one value
var array4 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one"},{"name":"two"},{"name":"two","value":0.3},{"name":"two"},{"name":"three"},{"name":"three"},{"name":"three"}];
//Should be true - last sequence contains one value
var array5 = [{"name":"one","value":0.33},{"name":"one","value":0.54},{"name":"one"},{"name":"two"},{"name":"two","value":0.3},{"name":"two", "value":0.3},{"name":"three"},{"name":"three"},{"name":"three","value":0.3}];

function containsUniqueValueForUniqueName(entry, index) {
  //So you can see in console how it short circuits the loop.
  console.log("Checking entry", entry);
  
  //If the value is a number, then increase the last count.
  if (!isNaN(entry.value)) {
    this.lastName = entry.name;
    this.lastCount += 1;
  }
  
  //When the lastName changes, or if its the last entry in the array, check if lastCount was 1. If yes, then exit true.
  if (this.lastName !== entry.name || index === array1.length - 1) {
    if (this.lastCount === 1)
      return true;
  
    //If its not 1, then we need to reset the count to 0.
    this.lastCount = 0;
  }
  
  return false;
}

var array1Result = array1.some(containsUniqueValueForUniqueName, {});
var array2Result = array2.some(containsUniqueValueForUniqueName, {});
var array3Result = array3.some(containsUniqueValueForUniqueName, {});
var array4Result = array4.some(containsUniqueValueForUniqueName, {});
var array5Result = array5.some(containsUniqueValueForUniqueName, {});

console.log('array1', array1Result);
console.log('array2', array2Result);
console.log('array3', array3Result);
console.log('array4', array4Result);
console.log('array5', array5Result);
Hide result

, , 1 .

. , , . , - - , .

+3

If you want to use lodash (here is a jsbin example ):

var array = [
  {"name": "one", "value": 0.33},
  {"name": "one", "value": 0.54},
  {"name": "one", "value": undefined},

  {"name": "two", "value": 0.3},
  {"name": "two", "value": undefined},
  {"name": "two", "value": undefined},

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

// groups objects by the key `name`
array = _.groupBy(array, function(obj) {
  return obj.name;
});

// gets the counts for each `name` of `value` properties that are numbers
array = _.mapValues(array, function(arr) {
  return arr.filter(function(obj) {
     return _.isNumber(obj.value);
  }).length;
});

console.log(array);

This will:

{
  one: 2,
  three: 0,
  two: 1
}

This tells you that the object with the name onehas 2 value, which are numbers, threehas 0 value, which are numbers, and twohas 1 value, which is a number.

0
source

All Articles