Skip to main content
deleted 8 characters in body
Source Link
Aliunco
  • 399
  • 4
  • 9

Here is another solution whichthat can return the differences, just like git diff: (it has been written in typescript, itif you're not using typescript version, simply just remove the types)

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is a sample of usage:

arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true} 

Here is another solution which can return the differences, just like git diff: (it has been written in typescript, it you're not using typescript version, simply just remove the types)

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is a sample of usage:

arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true} 

Here is another solution that can return the differences, just like git diff: (it has been written in typescript, if you're not using typescript version, just remove the types)

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is a sample of usage:

arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true} 
added 228 characters in body
Source Link
Aliunco
  • 399
  • 4
  • 9

Here is another solution which can return the differencedifferences, just like git diff: (it has been written in typescript, it you're not using typescript version, simply just remove the types)

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is a sample of usage:

arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true} 

Here is another solution which can return the difference, just like git diff:

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is another solution which can return the differences, just like git diff: (it has been written in typescript, it you're not using typescript version, simply just remove the types)

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } } 

Here is a sample of usage:

arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true} 
Source Link
Aliunco
  • 399
  • 4
  • 9

Here is another solution which can return the difference, just like git diff:

/** * util function to calculate the difference between two arrays (pay attention to 'from' and 'to'), * it would return the mutations from 'from' to 'to' * @param { T[] } from * @param { T[] } to * @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added, * false means removed */ export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } { var diff: { [x in string]: boolean } = {}; var newItems: T[] = [] diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) for (var i = 0; i < to.length; i++) { if (diff[JSON.stringify(to[i])]) { delete diff[JSON.stringify(to[i])] } else { newItems.push(to[i]) } } return { ...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}), ...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {}) } }