I have a master product list that has a logical object structure:
var myProducts = {
"productInfo":{
"productVariations":[{
"ID":XXXXXXX,
"Attributes":{
"edition":'professional',
"license":"perpetual"
}
},
{
"ID":XXXXXX,
"Attributes":{
"edition":'standard',
"license":"perpetual"
}
},
.
.
.
I am trying to compare this to a dynamically generated object array created by a product configurator application I have built. This list looks like this once generated:
var zcs_edition = [{ edition="standard", license="perpetual"}, { edition="professional", license="perpetual" }]
using $.inArray to compare the elements like below doesnt seem to be effective:
$.each(myProducts.productInfo.productVariations,function(i, val){
//console.log(this.productID);
//console.log(val.productAttributes );
//console.log($.inArray(val.productAttributes, zcs_edition ))
});
Am I doing something wrong here, I half expected this to work.
First off there are two errors in you example: In your master product list you have the element
Attributes, but in your code you are referring to it asproductAttribute, and in your listzcs_editionyou are using=instead of:.To your problem: You can’t compare objects like that in JavaScript.
{a: 1, b: 2} == {a: 1, b: 2}will always return false, because they are two different object, even it the happen to have the same properties and property values.You’d need to use a function that iterates over the properties and compares them one by one. See for example: Object comparison in JavaScript
This however means you can’t use
$.inArray, because it can’t use a such a function. You’d have to iterate of the array yourself, too.