Created
May 14, 2023 15:09
-
-
Save chakrit/a68bb5ea628839cfb61a19a6aae4f3c5 to your computer and use it in GitHub Desktop.
Revisions
-
chakrit created this gist
May 14, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ input: { outer: { inner: [ {item: "one", values: [3, 2, 1]}, {item: "two", values: [1, 2, 3]}, {item: "three", values: [7, 8, 9]}, ] innernope: "innernope" } unrelated: "unrelated" } path: ["outer", "inner", 1, "values"] append: 4 breakdown: [ for idx, p in path { if idx == 0 { input[p] } if idx > 0 { breakdown[idx-1][p] } }, ] reconstruct: [ for idx, _ in path { if (int & path[len(path)-idx-1]) == _|_ { { if idx < len(path)-1 { for key, value in breakdown[len(path)-idx-2] if key != path[len(path)-idx-1] { "\(key)": value } } if idx == len(path)-1 { for key, value in input if "\(key)" != "\(path[len(path)-idx-1])" { "\(key)": value } } if idx == 0 { "\(path[len(path)-1])": breakdown[len(path)-1] + [append] } if idx > 0 { "\(path[len(path)-idx-1])": reconstruct[idx-1] } } } if (int & path[len(path)-idx-1]) != _|_ { [ for key, value in breakdown[len(path)-idx-2] { if key != path[len(path)-idx-1] { value } if key == path[len(path)-idx-1] { if idx == 0 { breakdown[len(path)-1] + [append] } if idx > 0 { reconstruct[idx-1] } } }, ] } }, ] output: reconstruct[len(path)-1] & { outer: { inner: [ {item: "one", values: [3, 2, 1]}, {item: "two", values: [1, 2, 3, 4]}, {item: "three", values: [7, 8, 9]}, ] } }