1

I want to compare two sorted JavaScript arrays with custom objects in them and calculate the differences. I would like to do this using iterator objects to walk through them using next(). (Just as it is possible with iterators in Java.) In the MDN it says:

In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination. More specifically an iterator is any object which implements the Iterator protocol by having a next() method which returns an object with two properties: value, the next value in the sequence; and done, which is true if the last value in the sequence has already been consumed. If value is present alongside done, it is the iterator's return value. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)

Is there any convenient method to gain an iterator object from an array in JavaScript?

white_gecko
  • 4,808
  • 4
  • 55
  • 76
  • Is this just for fun/educational purposes? It won't get much easier/shorter than `if (!(arr1.length !== arr2.length || arr1.some((x, i) => x !== arr2[i])))` – Andreas Aug 07 '19 at 05:49
  • @Andreas thanks for this hint. Could you maybe explain this possibility a little bit verbose? Maybe in another Answer, for the reference. – white_gecko Aug 07 '19 at 06:06
  • I won't add it as an answer as you're explicitly asking for an iterator. But it's fairly simple, especially as this only has to work with sorted arrays. The first check is the length. If this doesn't match we don't need to check the elements. If the length matches we then check if any of the elements in the arrays don't match (based on their index) with [`Array.prototype.some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some). – Andreas Aug 07 '19 at 08:14
  • Ah ok now I understand it. I thought there would be more magic in it that I don't see. Actually I want to compare the arrays in order to calculate the differences and work with the diff afterwards. Thus your simple solution would not help here. (I just added this clarification to the question.) – white_gecko Aug 07 '19 at 09:02
  • https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – Andreas Aug 07 '19 at 09:23
  • Thanks @Andreas I've now also clarified, that I need it for custom objects not just a raw diff of flat arrays. – white_gecko Aug 07 '19 at 09:29

1 Answers1

3

Just like for all iterable objects, to obtain an iterator, access its Symbol.iterator property to get the generator, and call it to get the iterator:

const arr = ['a', 'b', 'c'];
const iter = arr[Symbol.iterator]();
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());

const arr1 = ['a', 'b', 'c'];
const arr2 = ['a', 'b', 'c'];
const iter1 = arr1[Symbol.iterator]();
const iter2 = arr2[Symbol.iterator]();

console.log(iter1.next());
console.log(iter2.next());
console.log(iter1.next());
console.log(iter2.next());
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320