Why Array.indexOf doesn't find identical looking objects - javascript

I have array with objects.
Something Like this:
var arr = new Array(
{x:1, y:2},
{x:3, y:4}
);
When I try:
arr.indexOf({x:1, y:2});
It returns -1.
If I have strings or numbers or other type of elements but object, then indexOf() works fine.
Does anyone know why and what should I do to search object elements in array?
Of course, I mean the ways except making string hash keys for objects and give it to array...

indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
You cannot use === to check the equability of an object.
As #RobG pointed out
Note that by definition, two objects are never equal, even if they have exactly the same property names and values. objectA === objectB if and only if objectA and objectB reference the same object.
You can simply write a custom indexOf function to check the object.
function myIndexOf(o) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].x == o.x && arr[i].y == o.y) {
return i;
}
}
return -1;
}
DEMO: http://jsfiddle.net/zQtML/

As nobody has mentioned built-in function Array.prototype.findIndex(), I'd like to mention that it does exactly what author needs.
The findIndex() method returns the index of the first element in the
array that satisfies the provided testing function. Otherwise -1 is
returned.
var array1 = [5, 12, 8, 130, 44];
function findFirstLargeNumber(element) {
return element > 13;
}
console.log(array1.findIndex(findFirstLargeNumber));
// expected output: 3
In your case it would be:
arr.findIndex(function(element) {
return element.x == 1 && element.y == 2;
});
Or using ES6
arr.findIndex( element => element.x == 1 && element.y == 2 );
More information with the example above: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

As noted, two objects are never equal, but references can be equal if they are to the same object, so to make the code do what you want:
var a = {x:1, y:2};
var b = {x:3, y:4};
var arr = [a, b];
alert(arr.indexOf(a)); // 0
Edit
Here's a more general specialIndexOf function. Note that it expects the values of the objects to be primitives, otherwise it needs to be more rigorous.
function specialIndexOf(arr, value) {
var a;
for (var i=0, iLen=arr.length; i<iLen; i++) {
a = arr[i];
if (a === value) return i;
if (typeof a == 'object') {
if (compareObj(arr[i], value)) {
return i;
}
} else {
// deal with other types
}
}
return -1;
// Extremely simple function, expects the values of all
// enumerable properties of both objects to be primitives.
function compareObj(o1, o2, cease) {
var p;
if (typeof o1 == 'object' && typeof o2 == 'object') {
for (p in o1) {
if (o1[p] != o2[p]) return false;
}
if (cease !== true) {
compareObj(o2, o1, true);
}
return true;
}
}
}
var a = new String('fred');
var b = new String('fred');
var arr = [0,1,a];
alert(specialIndexOf(arr, b)); // 2

This works without custom code
var arr, a, found;
arr = [{x: 1, y: 2}];
a = {x: 1, y: 2};
found = JSON.stringify(arr).indexOf(JSON.stringify(a)) > - 1;
// found === true
Note: this does not give the actual index, it only tells if your object exists in the current data structure

Those objects aren't equal.
You must implement your own function.
You may do that for example :
var index = -1;
arr.forEach(function(v, i) {
if (this.x==v.x && this.y==v.y) index=i;
}, searched);
where searched is one of your object (or not).
(I would implement it with a simple loop but it's prettier with foreach)

Because two separate objects are not === to each other, and indexOf uses ===. (They're also not == to each other.)
Example:
var a = {x:1, y:2};
var b = {x:1, y:2};
console.log(a === b);
=== and == test for whether their operands refer to the same object, not if they refer to equivalent objects (objects with the same prototype and properties).

Here's another solution, where you pass a compare function as a parameter :
function indexOf(array, val, from, compare) {
if (!compare) {
if (from instanceof Function) {
compare = from;
from = 0;
}
else return array.__origIndexOf(val, from);
}
if (!from) from = 0;
for (var i=from ; i < array.length ; i++) {
if (compare(array[i], val))
return i;
}
return -1;
}
// Save original indexOf to keep the original behaviour
Array.prototype.__origIndexOf = Array.prototype.indexOf;
// Redefine the Array.indexOf to support a compare function.
Array.prototype.indexOf = function(val, from, compare) {
return indexOf(this, val, from, compare);
}
You can then use it these way:
indexOf(arr, {x:1, y:2}, function (a,b) {
return a.x == b.x && a.y == b.y;
});
arr.indexOf({x:1, y:2}, function (a,b) {
return a.x == b.x && a.y == b.y;
});
arr.indexOf({x:1, y:2}, 1, function (a,b) {
return a.x == b.x && a.y == b.y;
});
The good thing is this still calls the original indexOf if no compare function is passed.
[1,2,3,4].indexOf(3);

Looks like you weren't interested in this type of answer, but it is the simplest to make for others who are interested:
var arr = new Array(
{x:1, y:2},
{x:3, y:4}
);
arr.map(function(obj) {
return objStr(obj);
}).indexOf(objStr({x:1, y:2}));
function objStr(obj) {
return "(" + obj.x + ", " + obj.y + ")"
}

Related

Checking if a Set contains a list in Javascript

I have a set that I've added a list to.
var a = new Set();
a.add([1]);
Now I check if [1] exists in a:
a.has([1]);
> false
Based on the spec, this might be happening because if type is same for two objects being compared:
Return true if x and y refer to the same object. Otherwise, return
false.
And this also happens:
[1] == [1];
> false
So it might be that the Set .has() is using == for comparing equality (not sure though). Is there a .contains() method for Set() in JS like Java has?
You can't compare references like arrays and objects for equality (you can compare values, though).
The way you're doing it, you're checking against a different reference, even though the values appear to be the same.
Do something like this:
var a = new Set();
var b = [1];
a.add(b);
a.has(b); // => true
Take a look at MDN's Equality comparisons and sameness.
So it might be that the Set .has() is using == for comparing equality (not sure though)
Not necessarily. [1] === [1] (using the strict equality operator) also returns false.
Is there a .contains() method for Set() in JS like Java has?
Not in the way you're imagining it. You can do some sort of deep comparison, as mentioned in the first link in this answer.
although both [1]'s have the same value, they are two different arrays, pointing to two different locations in memory, hence they are not equal.
you can use a basic loop to check
for (let item of mySet.keys()) {
item.toString() == [1].toString();
//true on match.
}
If you compare two arrays with the "==" operator they will never be equals. They are different instance.
If you want using an array object :
Define how two arrays are compared.
Make a subclass of Set and override the "has" method.
.
function eqArray (a1, a2) {
if (!(a1 instanceof Array && a2 instanceof Array))
return false;
if ( a1.length != a2.length)
return false;
for (var i = 0, n=a1.length; i < n; i++) {
if (a1[i] instanceof Array && a2[i] instanceof Array) {
if (!eqArray(a1[i], a2[i]))
return false;
}
else if (a1[i] != a2[i])
return false;
}
return true;
}
function MySet(elems) {
var set = new Set (elems);
set.__proto__ = MySet.prototype;
return set;
}
MySet.prototype = new Set;
MySet.prototype.has = function (elem) {
if (elem instanceof Array){
for (var v of this) {
if (v instanceof Array && eqArray(elem,v))
return true;
}
return false;
}else {
return Set.prototype.has.call (this, elem);
}
}
var a = new MySet();
a.add([1]);
a.has([1]); //true
eqArray([1],[1]); //true
Ready to use it with any object you need.
You just need to define how to compare them on a function
class SetDeep extends Set {
constructor (elems){
super(elems)
}
has(elem, compare) {
if (compare instanceof Function) {
return Array.from(this).some( setElem => compare(elem, setElem));
} else {
return super.has(elem);
}
}
}
Using fbohorquez eqArray:
function eqArray (a1, a2) {
if (!(a1 instanceof Array && a2 instanceof Array))
return false;
if ( a1.length != a2.length)
return false;
for (var i = 0, n=a1.length; i < n; i++) {
if (a1[i] instanceof Array && a2[i] instanceof Array) {
if (!eqArray(a1[i], a2[i]))
return false;
}
else if (a1[i] != a2[i])
return false;
}
return true;
}
var a = new SetDeep();
var b = [1];
a.add(b);
a.has(b); // true
var c = new SetDeep();
c.add(b);
c.has([1]); // false
c.has([1], eqA) // true
With an object:
function equalId (a, b) {
return a.id === b.id;
}
var objSet = new SetDeep([ { id: 'foo'}, {id: 'bar'} ]);
var foo = {id: 'foo' };
objSet.has(foo); // false;
objSet.has(foo, equalId); // true

Sort an array of objects in Javascript by key value

First time poster, long time reader. I’m having a problem sorting an array of objects, this is homework so I’m not asking for someone to write the code for me just point me in the right direction or show me my over sight. The object is to write a function to sort an array of objects when passing in an array and a key ie:
([{a:2},{b:2},{a:1},{a:3},{b:3},{b:1}], “a”)
Should return
[{a:1},{a:2},{a:3},{b:1},{b:2},{b:3}];
I can’t use anything like underscore.js or node.js
//example array to use
var testarr = [{a:2},{b:2},{a:1},{a:3},{b:3},{b:1}];
console.log("array sort test: should look like [{a:1},{a:2},{a:3},{b:1},{b:2},{b:3}]");
//first attempt
var sortArrayByKey = function (arr1, key) {
(arr1.sort(function(a,b){
return a[key] - b[key];}));
return arr1;
};
//still only returns testarr
console.log(sortArrayByKey(testarr, "a") );
//second attempt
var sortArrayByKey1 = function (array, key) {
var compareByKey = function (a, b) {
var x = a[key]; var y = b[key];
return x - y;
}
array.sort(compareByKey);
return array;
};
//still only returns testarr
console.log(sortArrayByKey1(testarr, “a”));
![pic of requirements in-case i'm describing it wrong photo
Here's my solution. I made it so you can also add more keys and sort them too.
Fiddle - http://jsfiddle.net/Q7Q9C/3/
function specialSort(arrayToSort, keyOrder) {
arrayToSort = arrayToSort.sort(function (a, b) {
for (var key in keyOrder) {
if (!keyOrder.hasOwnProperty(key)) {
continue;
}
var aKey = keyOrder[key];
if (typeof a[aKey] === "undefined" && typeof b[aKey] === "undefined") {
continue;
}
if (typeof a[aKey] !== "undefined" && typeof b[aKey] === "undefined") {
return -1;
}
if (typeof a[aKey] === "undefined" && typeof b[aKey] !== "undefined") {
return 1;
}
if (a[aKey] > b[aKey]) {
return 1;
}
else if (a[aKey] < b[aKey]) {
return -1;
}
}
return 0;
});
return arrayToSort;
}
var arrayToSort = [
{a:2},
{b:2},
{a:1},
{a:3},
{b:3},
{c:3},
{c:2},
{b:1}
];
var keyOrder = ["a", "b", "c"];
var sortedArray = specialSort(arrayToSort, keyOrder);
console.log(JSON.stringify(sortedArray));
Hmm.. this is a weird one. First you need to check if one of the keys is the priority key and sort based on that. Then if both keys are equal sort by the values. The problem is that there is no straightforward way to get the key but you can use the for .. in loop.
I'm going to assume that each object contains only one property otherwise the code will not make sense since property is unordered in objects:
function sortPreferredKey(arr,key) {
arr.sort(function(a,b){
// get the keys of each object
for (var a_key in a) {break}
for (var b_key in b) {break}
if (a_key != b_key) {
if (a_key == key) return 1;
else if (b_key == key) return -1;
else return 0;
}
return a[a_key] - b[b_key];
});
}
I may have gotten the order of sort wrong but you get the idea. It's really weird that you'd even need to do something like this.
This is the best I can come up with. It will sort all the elements with the given key to the front; the elements without the key will be in the back, but they'll be in an unpredictable order.
function sortArrayByKey(arr, key) {
function compareKey(a, b) {
if (a.hasOwnProperty(key)) {
if (b.hasOwnProperty(key)) {
return a[key] - b[key];
} else {
return -1;
}
} else if (b.hasOwnProperty(key)) {
return 1;
} else {
return 0;
}
}
arr.sort(compareKey);
return arr;
}
The documentation for the sort method is here. The compare function:
should be a function that accepts two arguments x and y and returns a
negative value if x < y, zero if x = y, or a positive value if x > y.
The function is passed the values in the array, so it's like calling the function with:
compareFunction({a:2},{b:2});
What you seem to want to do is sort on the property name first, then on the value. The problem with that is that you can't guarantee what order the property names are returned in. In this case, if you have exactly one own property for each object, you can do:
// Return first own property returned by in
// ORDER IS NOT GUARANTEED
function getPropName(o) {
for (var p in o) {
if (o.hasOwnProperty(p)) {
return p;
}
}
}
function specialSort(array, key) {
array.sort(function (a, b) {
var aProp = getPropName(a);
var bProp = getPropName(b);
// If properties are the same, compare value
if (aProp == bProp) {
return a[aProp] - b[bProp];
}
// Otherwise, compare keys
return aProp == key? -1 : bProp == key? 1 : aProp.charCodeAt(0) - bProp.charCodeAt(0);
});
return array;
}
The above will also sort any other keys (c, d, e, etc.) after the preferred key so:
var a = [{c:3},{a:2},{b:2},{c:2},{a:1},{a:3},{b:3},{b:1},{c:1}]
specialSort(a, 'b'); // [{b:1}, {b:2}, {b:3}, {a:1}, {a:2}, {a:3}, {c:1}, {c:2}, {c:3}]
Here's a solution that makes a guess what to do if neither object being compared in the array has the passed in comparison key:
var data = [{a:2},{b:2},{a:1},{a:3},{b:3},{b:1}];
function sortByProperty(array, propName) {
function findFirstProperty(obj) {
for (x in obj) {
if (obj.hasOwnProperty(x)) {
return x;
}
}
}
return array.sort(function(first, second) {
var firstHasProp = propName in first;
var secondHasProp = propName in second;
if (firstHasProp) {
if (secondHasProp) {
// both have the property
return first[propName] - second[propName];
} else {
// only first has the property
return -1;
}
} else if (secondHasProp){
// only second has the property
return 1;
} else {
// Neither sort candidate has the passed in property name
// It is not clear what you want to do here as no other property
// name has been specified
return first[findFirstProperty(first)] - second[findFirstProperty(second)]
}
});
}
Working demo: http://jsfiddle.net/jfriend00/PFurT/
Logically, here's what it does:
If both comparison candidates have the desired property, then simply sort by the value of that property.
If only one comparison candidate has the desired property, then make the one with the desired property be first in the sort order
If neither comparison candidate has the desired property, find the first other property on the object and sort by that. This is a guess because you don't really explain what you want to happen in this case, but it works for the data example you provided.
Here's a version that works like the above one, but is has been extended to sort properties that are not the passed in property in alpha order and to deal with empty objects (with no properties) so they go at the end of the sort:
var data = [{c:4},{a:2},{b:2},{a:1},{a:3},{b:3},{b:1},{},{c:3}];
function sortByProperty(array, propName) {
function findFirstProperty(obj) {
for (x in obj) {
if (obj.hasOwnProperty(x)) {
return x;
}
}
}
return array.sort(function(first, second) {
var firstHasProp = propName in first;
var secondHasProp = propName in second;
if (firstHasProp) {
if (secondHasProp) {
// both have the property
return first[propName] - second[propName];
} else {
// only first has the property
return -1;
}
} else if (secondHasProp){
// only second has the property
return 1;
} else {
// Neither sort candidate has the passed in property name
// It is not clear what you want to do here as no other property
// name has been specified
var firstProp = findFirstProperty(first);
var secondProp = findFirstProperty(second);
if (firstProp === undefined && secondProp === undefined) {
return 0;
} else if (firstProp === undefined) {
return 1;
} else if (secondProp === undefined) {
return -1;
}
else if (firstProp === secondProp) {
return first[firstProp] - second[secondProp];
} else {
return firstProp.localeCompare(secondProp);
}
}
});
}
Working demo: http://jsfiddle.net/jfriend00/6QsVv/

How to check the depth of an object?

I'm working on a permissions system with variable depth; depending on the complexity of a page, there could be more or less levels. I searched StackOverflow to find if this has been asked before, couldn't find it.
If I have this object:
{foo:{bar:{baz : 'baa'}}}
I need it to return 3, it has 3 levels to it.
With this object:
{abc: 'xyz'}
It would have to be 1.
This is what I have so far:
utils.depthOf = function(object, level){
// Returns an int of the deepest level of an object
level = level || 1;
var key;
for(key in object){
if (!object.hasOwnProperty(key)) continue;
if(typeof object[key] == 'object'){
level++;
level = utils.depthOf(object[key], level);
}
}
return level;
}
The problem is it counts sister elements too. It's actually not getting depth, it's counting all members of an object.
Well, here you go buddy, a function that does exactly what you need!
utils.depthOf = function(object) {
var level = 1;
for(var key in object) {
if (!object.hasOwnProperty(key)) continue;
if(typeof object[key] == 'object'){
var depth = utils.depthOf(object[key]) + 1;
level = Math.max(depth, level);
}
}
return level;
}
A lot easier than we thought it would be. The issue was how it was incremented, it shouldn't have been recursively adding, rather getting the bottom-most and adding one, then choosing the max between two siblings.
This old question was recently resurrected and I don't see any answers as simple as this one (to be fair, this uses techniques not available when the question was written):
const objectDepth = (o) =>
Object (o) === o ? 1 + Math .max (-1, ... Object .values(o) .map (objectDepth)) : 0
console .log (objectDepth ({foo: {bar: {baz: 'baa'}}}))
console .log (objectDepth ({abc: 'xyz'}))
This, like most answers here, will fail when the input object is cyclic. An answer that addresses that limitation would require much more sophistication.
Back from the dead! Throwing my solution into the mix -
function depth (t, mem = new Set)
{ if (mem.has(t))
return Infinity
else switch (mem.add(t), t?.constructor)
{ case Object:
case Array:
return 1 + Math.max
( -1
, ...Object
.values(t)
.map(_ => depth(_, mem))
)
default:
return 0
}
}
console.log(depth({a: {b: {c: "z"}}})) // 3
console.log(depth({a: "z"})) // 1
console.log(depth({})) // 0
console.log(depth("z")) // 0
console.log(depth({a: [{b: "z"}]})) // 3
const a = []
a[0] = a
console.log(depth(a)) // Infinity
We can use the reg:
function getHowManyLevel(obj) {
let res = JSON.stringify(obj).replace(/[^{|^}]/g, '')
while (/}{/g.test(res)) {
res = res.replace(/}{/g, '')
}
return res.replace(/}/g, '').length
}
This should do it, if you wanna keep it short:
function maxDepth(object) {
if (typeof object !== "object" || object === null) {
return 0;
}
let values = Object.values(object);
return (values.length && Math.max(...values.map(value => maxDepth(value)))) + 1;
}
I used a dirty but efficient way :
The good point is that there is no Regex in it, because regex is costly in process time
getObjectDepth = (object) => {
// json to array of parenthesis array: ['{','{','}','}',]
let string = JSON.stringify(object)
.split('')
.filter(char => ['{', '}'].includes(char) );
let currentDepth = 0;
let maxDepth = 0;
string.forEach(char => {
if (char === '{') currentDepth++;
if (char === '}') currentDepth--;
if (currentDepth > maxDepth) maxDepth = currentDepth;
});
return maxDepth
}
It will only work if the object has no parenthesis in a string value though.

Merge two javascript objects adding values of common properties

I have two or more javascript objects. I want to merge them adding values of common properties and then sort them in descending order of values.
e.g.
var a = {en : 5,fr: 3,in: 9}
var b = {en: 8,fr: 21,br: 8}
var c = merge(a,b)
c should then be like this:
c = {
fr: 24,
en: 13,
in:9,
br:8
}
i.e. both objects are merge, values of common keys are added and then keys are sorted.
Here's what I've tried:
var a = {en : 5,fr: 3,in: 9}
var b = {en: 8,fr: 21,br: 8}
c = {}
// copy common values and all values of a to c
for(var k in a){
if(typeof b[k] != 'undefined'){
c[k] = a[k] + b[k]
}
else{ c[k] = a[k]}
}
// copy remaining values of b (which were not common)
for(var k in b){
if(typeof c[k]== 'undefined'){
c[k] = b[k]
}
}
// Create a object array for sorting
var arr = [];
for(var k in c){
arr.push({lang:k,count:c[k]})
}
// Sort object array
arr.sort(function(a, b) {
return b.count - a.count;
})
but I dont think its good. So many loops :( It would be nice if someone can provide a less messy and good code.
In ES2015+, object properties are ordered (first by ascending numeric keys, then by insertion order for non-numeric keys). This is guaranteed by the specification if you use one of the methods for which iteration order is specified (like Object.getOwnPropertyNames).
In ES2020+, the methods for which enumeration order used to be unspecified are now specified (though environments have been following it for ages anyway).
But you have to be sure that none of the properties are numeric (otherwise, they'll come first, before non-numeric properties, no matter the insertion order).
Use reduce to iterate over each object and create or add to the same property on the accumulator. Then, sort the object's entries, and use Object.fromEntries to transform it into an object with sorted properties. No need for jQuery:
var a = {en : 5,fr: 3,in: 9}
var b = {en: 8,fr: 21,br: 8}
console.log(merge(a, b));
function merge(...objects) {
const merged = objects.reduce((a, obj) => {
Object.entries(obj).forEach(([key, val]) => {
a[key] = (a[key] || 0) + val;
});
return a;
}, {});
return Object.fromEntries(
Object.entries(merged).sort(
(a, b) => b[1] - a[1]
)
);
}
It is not possible to sort the properties of an object, you can however sort an array:
var merged = $.extend({}, a);
for (var prop in b) {
if (merged[prop]) merged[prop] += b[prop];
else merged[prop] = b[prop];
}
// Returning merged at this point will give you a merged object with properties summed, but not ordered.
var properties = [];
for (var prop in merged) {
properties.push({
name: prop,
value: merged[prop]
});
}
return properties.sort(function(nvp1, nvp2) {
return nvp1.value - nvp2.value;
});
EDIT - i modified the script, this merges the properties if they are of the same type: numbers are summed, strings are concatenated and objects are recursively merged. I didn't include sorting because (quoting this answer Sorting JavaScript Object by property value)
JavaScript objects are unordered by definition (see the ECMAScript
Language Specification, section 8.6). The language specification
doesn't even guarantee that, if you iterate over the properties of an
object twice in succession, they'll come out in the same order the
second time.
If you need things to be ordered, use an array and the
Array.prototype.sort method.
function is_object(mixed_var) {
if (Object.prototype.toString.call(mixed_var) === '[object Array]') {
return false;
}
return mixed_var !== null && typeof mixed_var == 'object';
}
function merge(a, b) {
var cache = {};
cache = unpackObject(a, cache);
cache = unpackObject(b, cache);
return cache;
}
function unpackObject(a, cache) {
for (prop in a) {
if (a.hasOwnProperty(prop)) {
if (cache[prop] === undefined) {
cache[prop] = a[prop];
} else {
if (typeof cache[prop] === typeof a[prop]) {
if (is_object(a[prop])) {
cache[prop] = merge(cache[prop], a[prop]);
} else {
cache[prop] += a[prop];
}
}
}
}
}
return cache;
}
var a = {
en: 5,
fr: 3,
in : 9,
lang: "js",
object: {nestedProp: 6}
}
var b = {
en: 8,
fr: 21,
br: 8,
lang: "en",
object: {nestedProp: 1, unique: "myne"}
}
var c = merge(a, b);
fiddle here http://jsfiddle.net/vyFN8/1/
Here is my attempt, which is recursive for nested objects - https://gist.github.com/greenafrican/19bbed3d8baceb0a15fd
// Requires jQuery
// Merge nested objects and if the properties are numbers then add them together, else
// fallback to jQuery.extend() result
function mergeObjectsAdd(firstObject, secondObject) {
var result = $.extend(true, {}, firstObject, secondObject);
for (var k in result) {
if ("object" === typeof result[k]) {
firstObject[k] = firstObject[k] || {};
secondObject[k] = secondObject[k] || {};
result[k] = mergeObjectsAdd(firstObject[k], secondObject[k]);
} else {
firstObject[k] = firstObject[k] || 0;
secondObject[k] = secondObject[k] || 0;
result[k] = ("number" === typeof firstObject[k] && "number" === typeof secondObject[k]) ? (firstObject[k] + secondObject[k]) : result[k];
}
}
return result;
}

JavaScript: Comparing objects as data structures

I need to do operations like compare that two number arrays have the same values, or two objects have the same member values. Is there a method that does this for me already (either ECMAScript or jQuery)?
For example, the function should return true that these both are the same:
var a = [1,2,3,4];
var b = [1,2,3,4];
Or with objects, the function should return that these two are the same:
var a = { id: 99, name: "Chris" };
var b = { name: "Chris", id: 99 };
I'm thinking maybe just JSON.stringify(a) === JSON.stringify(b)? Any problem with that?
Try the equiv library by Philippe Rathé. (Unfortunately the main site seems to be down, so I linked to js-test-driver source.)
Here's a pretty silly way to compare two objects containing scalar values:
function same(object1, object2) {
var merged = $.extend({}, object1, object2);
for (var key in merged) {
if (!object1.hasOwnProperty(key) ||
!object2.hasOwnProperty(key) ||
object1[key] != merged[key]) {
return false;
}
}
return true;
}
It relies on jQuery's extend and fails if object2 inherits properties from a prototype, since extend copies those, too.
function areEquiv ( a, b ) {
var eqCount = 0,
sizeCount = 0,
othCount = 0,
i;
if (typeof a.concat === "function") { // if comparing arrays
for (i = 0; i < a.length; i++) {
othCount = sizeCount = sizeCount + 1;
if (a[i] === b[i]) eqCount++;
}
} else { // if comparing objects
for (i in a) {
if (a.hasOwnProperty(i)) {
sizeCount = sizeCount + 1;
if (a[i] === b[i]) eqCount++;
}
}
for (i in b) {
if (b.hasOwnProperty(i)) {
othCount = othCount + 1;
}
}
}
return (a.length == b.length && eqCount == sizeCount && sizeCount == othCount);
}
Note that this will only work as long as the array or object contains simple values and isn't an array of objects or object with a parameter that's also an object.

Categories

Resources