0

I have two objects from the same constructor, they differ the first and second properties, but all the rest match. I want to confirm that in the code.

The JSON.stringify is good to compare whole objects, but not so good with starting from a certain property. I tried creating two variables one for each object, strigifying the first and second properties and adding them together in one string, for each variable.

And THEN I compare the two objects stringifying them by using substring starting from the variables LENGTH like this:

function Car(carname, price, performance, efficiency, offroad, size, twoseat, legroom, trunkspace, electric) {
    this.carname = carname; this.price = price; this.performance = performance; 
 this.efficiency = efficiency; this.offroad = offroad; this.size = size; 
 this.twoseat = twoseat; this.legroom = legroom; this.trunkspace = trunkspace; this.electric = electric;}




    var userParameters = new Car("!", 1,  null, true, false, true, false, false, false, false);
    var fiat500 = new Car("Fiat 500", 19000, null, true, false, true, false, false, false, false);

        var n = JSON.stringify(userParameters.carname) + JSON.stringify(userParameters.price);
        var m = JSON.stringify(fiat500.carname) + JSON.stringify(fiat500.price);
        var stringCar = JSON.stringify(fiat500);
        var stringUser = JSON.stringify(userParameters);
        if ((stringCar.substring(n.length)) === (stringUser.substring(m.length))) {
            //Do stuff in case equal
        }

It looks like it would be the solution, but it doesn't work. Halp

Hiago Serpa
  • 41
  • 1
  • 7
  • 1
    *"The JSON.stringify is good to compare whole objects"* No, it isn't. It is not reliable, as property order can give you two different strings for equivalent objects. – T.J. Crowder Feb 19 '16 at 02:58
  • "*… they differ the first and second properties…*" since object properties don't have an order, concepts like "first" and "second" don't apply. – RobG Feb 19 '16 at 03:01
  • So if you stringify two identical objects, it will give two different strings because the properties orders are arranged randomly? – Hiago Serpa Feb 19 '16 at 03:03
  • Well I've tested the stringify on the objects and they always return the same string, with the same property order. Don't quite get whay you guys are saying. – Hiago Serpa Feb 19 '16 at 03:26
  • @HiagoSerpa—ECMA-262 does not specify an order (see [`[[Enumerate]]`](http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-enumerate). Objects generally return properties in the order they are added, but not necessarily. Array properties are usually numeric first in sequence, but maybe not. At one time, Opera returned them in alphabetic order. Deleting then replacing a property may or may not change its position in the sequence, and so on. See [*Object property order*](http://stackoverflow.com/search?q=%5Bjavascript%5D+object+property+order). – RobG Feb 19 '16 at 03:58

0 Answers0