0

Let say we have an object:

message { 
  diff: { 
    cat: "Tabby"
    name: "Meow Meow"
  }
}

Can you check duplicates with hasOwnProperty() and do a removal of the duplicates? Example

for (key in message.diff) {
   //outputs every change when all keys are updated
   if (message.diff.hasOwnProperty(key)){
     this.uniqueArray.push(key); 
   }
}

EDIT: I realized there maybe confusion what I am trying to talk about. I have a function that listens to any form element that can be updated which logs the input into console. It is mostly listening to the property names This outputs itself to array which may or may not contain duplicates of names.

If we updated the form fields many, many times, it becomes:

Current Output: ['name', 'name', 'name', 'cat']
Desired Ouput: ['name', 'cat']
Demon
  • 826
  • 7
  • 22

2 Answers2

1

Check that object key doesn't exist in array

for (key in message.diff) {
   //outputs every change when all keys are updated
   if (message.diff.hasOwnProperty(key) &&  this.uniqueArray.indexOf(key) == -1){
     this.uniqueArray.push(key); 
   }
}
Pavlo Zhukov
  • 3,007
  • 3
  • 26
  • 43
  • You should also drop the `hasOwnProperty` call altogether (or [do it](http://stackoverflow.com/a/13296897/1048572) [properly](http://eslint.org/docs/rules/guard-for-in)) – Bergi Apr 25 '17 at 22:01
  • @Bergi what do you mean by this. Please give an example. Or why `Object.hasOwnProperty(key)` is incorrect? I always use it in that way – Pavlo Zhukov Apr 25 '17 at 22:03
  • This is very helpful. – Demon Apr 25 '17 at 22:50
0

You can use Object.keys(), Array.prototype.filter(), Array.prototype.includes() to check if property of object is an element of array, Array.prototype.push(), spread element

var message = { 
  diff: { 
    cat: "Tabby",
    name: "Meow Meow"
  }
}

var arr = ["name", "cat"];

arr.push(...Object.keys(message.diff).filter(function(diff) {return !arr.includes(diff)}));

console.log(arr);

var message = { 
  diff: { 
    lion: "Tabby",
    tiger: "Meow Meow"
  }
}

arr.push(
  ...Object.keys(message.diff)
  .filter(function(diff) {return !arr.includes(diff)}))

console.log(arr);
guest271314
  • 1
  • 15
  • 104
  • 177
  • I don't think I need to add any extra property names. – Demon Apr 25 '17 at 22:28
  • @Demon What do you mean? No elements are pushed to array `arr` unless the element is not an element within array `arr`. The first declaration of `message` demonstrates that the duplicate property names are not pushed to `arr`. The second declaration of `message` has unique property names as to elements of `arr`; where the property names are unique to `arr`, unique properties names of object `message.diff` are pushed to `arr`. That is requirement, yes? – guest271314 Apr 25 '17 at 22:31
  • The result being that array `arr` should not contain duplicate elements. _"removing duplicates with hasOwnProperty"_ The elements are not set at array, thus there should be no duplicates within array to remove. – guest271314 Apr 25 '17 at 22:37
  • @Demon Are you trying to delete the property from the object? If not trying to prevent duplicate elements in array or delete property from object, not sure what Question is? – guest271314 Apr 25 '17 at 22:44
  • My bad. The second example confused me. Yeah that is what I am trying to do. I am trying to prevent any duplicate elements in the array. – Demon Apr 25 '17 at 22:45
  • 1
    That is what `javascript` at Question achieves. If spread element and or `.includes()` is not defined you can use `.indexOf()` and `.concat()` of `Array.prototype`. – guest271314 Apr 25 '17 at 22:48
  • @Demon `arr.push.apply(arr, Object.keys(message.diff).filter(function(diff) {return arr.indexOf(diff) ==-1}))` – guest271314 Apr 25 '17 at 22:57