Compare two objects properties in JS

后端 未结 5 1867
面向向阳花
面向向阳花 2021-01-21 13:56

I need to compare two objects, and find out what properties are missing. The objects are fairly big, with several levels.

I will give short example of the type of object

5条回答
  •  梦毁少年i
    2021-01-21 14:34

    If your situation allows it I'd suggest using http://underscorejs.org/ library, rather than rolling your own solution (or go look at their implementation). In JS deep object comparison is sometimes not trivial. If you decide to roll your own solution, you would recursively iterate through the properties and compare them one by one (ignoring native / built-in object properties and perhaps inherited from some prototype).

    I'll gladly elaborate if you'd like.

提交回复
热议问题