JavaScript Return error if some “familiar” fields within an object are not adjacent

Assuming your JavaScript engine retains ordering of members (most of them does, but they don't have to, see this question):

function hasError(dataObj) {
  var error = false;
  var previousGroupID = -1;
  var thisGroupID = -1;
  var seenGroups = new Set();

  for (index in dataArray) {
    thisGroupID = dataObj[index].groupID;
    if (previousGroupID !== thisGroupID) {
      if (seenGroups.has(thisGroupID)) {
        if (thisGroupID !== 0) error = true;
      } else {
        seenGroups.add(thisGroupID);
      }
    }
    previousGroupID = thisGroupID;
  }
  return error;
}

Otherwise, you can use Map, which is guaranteed to retain entry ordering:

function hasError(dataMap) {
  var error = false;
  var previousGroupID = -1;
  var thisGroupID = -1;
  var seenGroups = new Set();
  var thisGroup = null;
  for (thisGroup of dataMap.values()) {
    thisGroupID = thisGroup.groupID;
    if (previousGroupID !== thisGroupID) {
      if (seenGroups.has(thisGroupID)) {
        if (thisGroupID !== 0) error = true;
      } else {
        seenGroups.add(thisGroupID);
      }
    }
    previousGroupID = thisGroupID;
  }
  return error;
}

var m = new Map();
m.set('hre', { groupID: 1 });
m.set('bla', { groupID: 1 });
m.set(3, { groupID: 0 });
m.set(4, { groupID: 2 });
m.set(5, { groupID: 0 });
m.set(1, { groupID: 1 });
console.log(hasError(m));

Or you can break it down into separate functions:

function listHasError(list) {
  var error = false;
  var previousGroupID = -1;
  var thisGroupID = -1;
  var seenGroups = new Set();
  var thisGroup = null;
  list.forEach(function(thisGroupID) {
    if (previousGroupID !== thisGroupID) {
      if (seenGroups.has(thisGroupID)) {
        if (thisGroupID !== 0) error = true;
      } else {
        seenGroups.add(thisGroupID);
      }
    }
    previousGroupID = thisGroupID;
  });
  return error;
}
function extractGroupsFromMap(dataMap) {
  var res = [];
  for (thisGroup of dataMap.values()) {
    res.push(thisGroup.groupID);
  }
  return res;
}
function extractGroupsFromObj(dataObj) {
  var res = [];
  for (index in dataObj) {
    res.push(dataObj[index].groupID);
  }
  return res;
}

var dataObj = {
  'hre': {groupID: 1},
  'bla': {groupID: 1},
  3: {groupID: 0},
  4: {groupID: 2},
  16: {groupID: 2},
  6: {groupID: 2},
  7: {groupID: 0},
  64893: {groupID: 0},
  9: {groupID: 1}
};
console.log(listHasError(extractGroupsFromObj(dataObj)));
Answer:1

I have the following code I received: function sortByProp(...props) { const callback = props.pop(); return function(a, b) { const v1 = retrieve(props, a); const v2 = retrieve(props, b); ...

I have the following code I received: function sortByProp(...props) { const callback = props.pop(); return function(a, b) { const v1 = retrieve(props, a); const v2 = retrieve(props, b); ...

  1. javascript spread operator alternative
  2. kotlin spread operator alternative
  3. es5 spread operator alternative
  4. object spread operator alternative
  5. groovy spread operator alternative

I have JavaScript code with object like: var myObj = { SomeVal: 1, Sizzle: function(){ //sizzle } }; How can I include Sizzle selector engine as an function in object? In Sizzle ...

I have JavaScript code with object like: var myObj = { SomeVal: 1, Sizzle: function(){ //sizzle } }; How can I include Sizzle selector engine as an function in object? In Sizzle ...

I'm trying to simulate brushes with the HTML canvas element. To get brush hardness, I'm using a radial gradient, but I'm not entirely sure whether it's faster to create a new radial gradient for every ...

I'm trying to simulate brushes with the HTML canvas element. To get brush hardness, I'm using a radial gradient, but I'm not entirely sure whether it's faster to create a new radial gradient for every ...

  1. canvas radial gradient generator
  2. canvas radial gradient circle
  3. canvas radial gradient
  4. canvas radial gradient parameters
  5. canvas radial gradient example
  6. html canvas radial gradient
  7. canvas create radial gradient
  8. html5 canvas radial gradient
  9. js canvas radial gradient
  10. javascript canvas radial gradient
  11. canvas fillstyle radial gradient
  12. android canvas radial gradient
  13. canvas arc radial gradient
  14. mdn canvas radial gradient
  15. canvas draw circle with radial gradient

Example: var readBackend = function(){ var deferred = q.defer(); readData().then(function(data) { deferred.resolve(data); }) return deferred.promise; } class Test { ...

Example: var readBackend = function(){ var deferred = q.defer(); readData().then(function(data) { deferred.resolve(data); }) return deferred.promise; } class Test { ...

  1. asynchronous data loading with new kotlin flow
  2. asynchronous data loading
  3. asynchronous data loading in android
  4. asynchronous data loading c#
  5. devexpress asynchronous data loading
  6. datagridview asynchronous data loading
  7. pytorch asynchronous data loading
  8. angular asynchronous data loading