1

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.

designmode
  • 143
  • 2
  • 12
  • In case you are willing to use underscore.js you could use its `_.isEqual` method. Works fine. See: http://documentcloud.github.com/underscore/#isEqual – m90 Mar 13 '12 at 09:27

2 Answers2

0

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 as productAttribute, and in your list zcs_edition you 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.

Community
  • 1
  • 1
RoToRa
  • 37,635
  • 12
  • 69
  • 105
0
var zcs_edition = [{ edition="standard", license="perpetual"}, { edition="professional", license="perpetual" }]

{ edition="standard", license="perpetual"} is not a valid Object

it should be { edition:"standard", license:"perpetual"}

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Leo
  • 1