Skip to main content
work-around the markdown "code after list" bug.
Source Link
Quentin Pradet
  • 6.9k
  • 1
  • 24
  • 44
  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

    This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.
  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

    b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.
  3. There is no duplication since ids are unique.

    There is no duplication since ids are unique.
  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

    I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

The code:

function areDifferentByIds(a, b) {
    var idsA = getIds(a);
    var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
}
  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.
  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.
  3. There is no duplication since ids are unique.
  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

The code:

function areDifferentByIds(a, b) {
    var idsA = getIds(a);
    var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
}
yet another impl.
Source Link
Quentin Pradet
  • 6.9k
  • 1
  • 24
  • 44

I don't think your main logic can be changed, but you can still write this more concisely:

function areDifferentByIds(a, b) {
    var idsA = getIds(a);
    var idsB = getIds(b);

    return idsA.length == idsB.length
      && idsA.every(function(e,i) { return idsB.indexOf(e) == i; });
}

Note that every and reduce only work since IE9. An implementation is proposed on MDN's Array.every page. It is still ES5 though.


(For the comments below to make sense, I kept the nonsense below).

Do you know about ES5ES5's reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Update:

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

Did I miss anything else? edit: OK, ["x"] versus ["x", "y"] won't work. I'm back to work.

Do you know about ES5 reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Update:

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

Did I miss anything else? edit: OK, ["x"] versus ["x", "y"] won't work. I'm back to work.

I don't think your main logic can be changed, but you can still write this more concisely:

function areDifferentByIds(a, b) {
    var idsA = getIds(a);
    var idsB = getIds(b);

    return idsA.length == idsB.length
      && idsA.every(function(e,i) { return idsB.indexOf(e) == i; });
}

Note that every and reduce only work since IE9. An implementation is proposed on MDN's Array.every page. It is still ES5 though.


(For the comments below to make sense, I kept the nonsense below).

Do you know about ES5's reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Update:

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

Did I miss anything else? edit: OK, ["x"] versus ["x", "y"] won't work. I'm back to work.

fix holes.
Source Link
Quentin Pradet
  • 6.9k
  • 1
  • 24
  • 44

Do you know about ES5 reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Update:

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

Did I miss anything else? edit: OK, ["x"] versus ["x", "y"] won't work. I'm back to work.

Do you know about ES5 reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Do you know about ES5 reduce() function? It runs through every value of the array (x in my code), letting you know the current index (i in my code), and asks you to "reduce" your array using an accumulator (acc in my code).

The accumulator is first set to true, and is still true only if the current indices are equal.

function areDifferentByIds(a, b) {
    return a.reduce(function(acc,x,i) { return acc && a[i] == b[i] }, true);
}

Here is a simpler example of reduce which could help you understand how it works. This one sums the elements of my array:

sum = [1, 2, 3, 4].reduce(function(acc, x) { return acc + x}, 0);

Update:

  1. This new version does compare id properties. I don't think you can avoid the getIds() step anyway, which is why it wasn't in the previous version.

  2. b being longer than a wasn't a problem. b being shorter than a could have been one: with a = [undefined], a[20] == a[0] is true. Using indexOf to search elements means I no longer have to deal with that.

  3. There is no duplication since ids are unique.

  4. I switched to every(), which does the same thing that my previous reduce() implementation, but is way simpler.

    function areDifferentByIds(a, b) { var idsA = getIds(a); var idsB = getIds(b);

    return a.every(function(e, i) { return b.indexOf(e) == i }
    

    }

Did I miss anything else? edit: OK, ["x"] versus ["x", "y"] won't work. I'm back to work.

Source Link
Quentin Pradet
  • 6.9k
  • 1
  • 24
  • 44
Loading