Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

c# compare two objects for changes

public static JObject FindDiff(this JToken Current, JToken Model)
{
    var diff = new JObject();
    if (JToken.DeepEquals(Current, Model)) return diff;

    switch(Current.Type)
    {
        case JTokenType.Object:
            {
                var current = Current as JObject;
                var model = Model as JObject;
                var addedKeys = current.Properties().Select(c => c.Name).Except(model.Properties().Select(c => c.Name));
                var removedKeys = model.Properties().Select(c => c.Name).Except(current.Properties().Select(c => c.Name));
                var unchangedKeys = current.Properties().Where(c => JToken.DeepEquals(c.Value, Model[c.Name])).Select(c => c.Name);
                foreach (var k in addedKeys)
                {
                    diff[k] = new JObject
                    {
                        ["+"] = Current[k]
                    };
                }
                foreach (var k in removedKeys)
                {
                    diff[k] = new JObject
                    {
                        ["-"] = Model[k]
                    };
                }
                var potentiallyModifiedKeys = current.Properties().Select(c => c.Name).Except(addedKeys).Except(unchangedKeys);
                foreach (var k in potentiallyModifiedKeys)
                {
                    var foundDiff = FindDiff(current[k], model[k]);
                    if(foundDiff.HasValues) diff[k] = foundDiff;
                }
            }
            break;
        case JTokenType.Array:
            {
                var current = Current as JArray;
                var model = Model as JArray;
                var plus = new JArray(current.Except(model, new JTokenEqualityComparer()));
                var minus = new JArray(model.Except(current, new JTokenEqualityComparer()));
                if (plus.HasValues) diff["+"] = plus;
                if (minus.HasValues) diff["-"] = minus;
            }
            break;
        default:
            diff["+"] = Current;
            diff["-"] = Model;
            break;
    }

    return diff;
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: Lists - Learn C# 
Typescript :: dart exit loop 
Typescript :: Round a float two decimal points 
Typescript :: npm run scripts does not work 
Typescript :: serenity.is hide form field 
Typescript :: angular build router-outlet not working 
Typescript :: msgpack lite 
Typescript :: class-validator not working nest-typescript-starter 
Typescript :: mongoose model enum 
Typescript :: stripe create subscription 
Typescript :: merge to datasets in r 
Typescript :: scripted testing and exploratory testing 
Typescript :: pandas get count of pair of elements in two columns 
Typescript :: outputs i angular 
Typescript :: react native 3 dots icon 
Typescript :: listen to server sent events flutter 
Typescript :: stackoverflow ngbdate angular 
Typescript :: writing multiple functional components in single file in react 
Typescript :: typescript add object to object 
Typescript :: arrays in typescript 
Typescript :: Electron WebContents context-menu 
Typescript :: how to list elements of an array C# 
Typescript :: date formats in mongodb 
Typescript :: Create 2 set A and B of size n1 and n2 . Print sets A and B. 
Typescript :: apexcharts marker onclick 
Typescript :: how to run springboots processbuilder 
Typescript :: how to compare two entity objects in c# to update 
Typescript :: different keymaps in the following locations 
Typescript :: What types of Collections/Data structures you have used 
Typescript :: typescript find non matching objects in two arrays 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =