Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created November 12, 2013 08:10
Show Gist options
  • Select an option

  • Save timoxley/7427294 to your computer and use it in GitHub Desktop.

Select an option

Save timoxley/7427294 to your computer and use it in GitHub Desktop.
how to do deep equal on sparse array?
var require('assert')
var a = []
// make sparse array
a[20] = true
var b = []
// try copying a's items into b with splice
b.splice.apply(b, [0, 0].concat(a.slice(0, 21)))
assert.deepEqual(a, b) // throws?
console.log(b)
/*
[ undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
true ]
*/
console.log(a)
/*
[ , , , , , , , , , , , , , , , , , , , , true ]
*/
console.log(a[0] === b[0])
/*
true
*/
@timoxley

Copy link
Copy Markdown
Author

Ahh, I've got it.

b has numeric keys 0 - 20. a does not. assert.deepEqual must do something related to the keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment