[thelist] Diff algorithm in JavaScript
Simon Perry
simon.perry at si-designs.co.uk
Thu Nov 27 17:49:56 CST 2003
Fernando Gomez wrote:
>
> In a JavaScript application I'm developing, the user is prompted if he
> wants to save the changes he's made to the data in a (complex) data
> entry form, before switching to another task.
>
> Given the complexity of the data entry form, I've just thought that it
> would be nice to offer him a list of the differences between the
> actual data and the original data, so that he can decide in case of
> doubt.
>
> The data I'm taking about is compared not element by element of the
> form; instead I build a long string with all the data in the form,
> something like
>
> "data line 1\ndata line 2\ndata line 3\ndata line 4\n..."
>
> and then compare this string with the original one.
Fernando,
Javascript supports hashes[1] so the approach I would take would be to
create two hash objects to store your form data. Then iterate through
the original hash to create a new array of data sets that have been
added then again for those that have been removed.
Simon
Sudo code:
for (var n in myOriginalHashtable) {
//compare old and new hashes and pop the differences into a new array
}
[1] http://www.crockford.com/javascript/survey.html
More information about the thelist
mailing list