-
Notifications
You must be signed in to change notification settings - Fork 17
Snips to merge objects in different languages #74 #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VaradJadhav
wants to merge
1
commit into
rahulrk-dev:main
Choose a base branch
from
VaradJadhav:snips-merge-objects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,36 +3,143 @@ Merges two or more objects. | |
|
|
||
| ## Result | ||
| ```js | ||
| merge({ a: [1,2,3], b: 1 }, { a: 4, c: 'bar' }); // { a: [1,2,3,4], b: 1, c: 'bar' } | ||
| merge({ a: [1, 2, 3], b: 1 }, { a: 4, c: "bar" }); // { a: [1,2,3,4], b: 1, c: 'bar' } | ||
| ``` | ||
|
|
||
| ## Snip Langauge | ||
| * [JavaScript](#javascript) | ||
| - [JavaScript](#javascript) | ||
| - [Python](#python) | ||
| - [Java] (#java) | ||
| - [CPP] (#cpp) | ||
|
|
||
| ### JavaScript | ||
| ```js | ||
| const merge = (...arr) => { | ||
| arr = arr.filter(obj => typeof obj == 'object' && obj !== null) | ||
| const keys = [], result = {} | ||
| arr.forEach(obj => { | ||
| Object.keys(obj).forEach(key => { | ||
| if(!keys.includes(key)) keys.push(key) | ||
| }) | ||
| }) | ||
|
|
||
| for(const key of keys){ | ||
| let newValue | ||
| for(const obj of arr){ | ||
| if(Array.isArray(obj[key])){ | ||
| if(Array.isArray(newValue)) | ||
| newValue = newValue.concat(obj[key].filter(x => !newValue.includes(x))) | ||
| else newValue = obj[key] | ||
| } | ||
| else if(Array.isArray(newValue)) newValue.push(obj[key]) | ||
| else if(obj[key]) newValue = obj[key] | ||
| } | ||
| result[key] = newValue | ||
| const merge = (...arr) => { | ||
| arr = arr.filter((obj) => typeof obj == "object" && obj !== null); | ||
| const keys = [], | ||
| result = {}; | ||
| arr.forEach((obj) => { | ||
| Object.keys(obj).forEach((key) => { | ||
| if (!keys.includes(key)) keys.push(key); | ||
| }); | ||
| }); | ||
|
|
||
| for (const key of keys) { | ||
| let newValue; | ||
| for (const obj of arr) { | ||
| if (Array.isArray(obj[key])) { | ||
| if (Array.isArray(newValue)) | ||
| newValue = newValue.concat( | ||
| obj[key].filter((x) => !newValue.includes(x)) | ||
| ); | ||
| else newValue = obj[key]; | ||
| } else if (Array.isArray(newValue)) newValue.push(obj[key]); | ||
| else if (obj[key]) newValue = obj[key]; | ||
| } | ||
| return result | ||
| result[key] = newValue; | ||
| } | ||
| return result; | ||
| }; | ||
| ``` | ||
|
|
||
| ### Python | ||
| ```python | ||
| def merge(*args): | ||
| args = [obj for obj in args if isinstance(obj, dict)] | ||
| keys, result = set(), {} | ||
|
|
||
| for obj in args: | ||
| keys.update(obj.keys()) | ||
|
|
||
| for key in keys: | ||
| new_value = None | ||
| for obj in args: | ||
| if isinstance(obj.get(key), list): | ||
| if isinstance(new_value, list): | ||
| new_value += [x for x in obj[key] if x not in new_value] | ||
| else: | ||
| new_value = obj[key] | ||
| elif isinstance(new_value, list): | ||
| new_value.append(obj[key]) | ||
| elif obj.get(key): | ||
| new_value = obj[key] | ||
| result[key] = new_value | ||
|
|
||
| return result | ||
| ``` | ||
|
|
||
| ### Java | ||
| ```java | ||
| public static Map<String, Object> merge(Map<String, Object>... maps) { | ||
| Map<String, Object> result = new HashMap<>(); | ||
| Set<String> keys = new HashSet<>(); | ||
|
|
||
| for (Map<String, Object> map : maps) { | ||
| keys.addAll(map.keySet()); | ||
| } | ||
|
|
||
| for (String key : keys) { | ||
| Object newValue = null; | ||
| for (Map<String, Object> map : maps) { | ||
| if (map.containsKey(key)) { | ||
| Object value = map.get(key); | ||
| if (value instanceof List) { | ||
| if (newValue instanceof List) { | ||
| ((List) newValue).addAll((List) value); | ||
| } else { | ||
| newValue = new ArrayList<>((List) value); | ||
| } | ||
| } else if (newValue instanceof List) { | ||
| ((List) newValue).add(value); | ||
| } else { | ||
| newValue = value; | ||
| } | ||
| } | ||
| } | ||
| result.put(key, newValue); | ||
| } | ||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| ### CPP | ||
| ```cpp | ||
| using Value = variant<int, string, vector<int>>; | ||
| using Dictionary = map<string, Value>; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See if you can do it without any external dependancy |
||
|
|
||
| Dictionary merge(vector<Dictionary> arr) { | ||
| set<string> keys; | ||
| Dictionary result; | ||
|
|
||
| for (const auto& obj : arr) { | ||
| for (const auto& [key, value] : obj) { | ||
| keys.insert(key); | ||
| } | ||
| } | ||
|
|
||
| for (const string& key : keys) { | ||
| Value newValue; | ||
| for (const auto& obj : arr) { | ||
| if (obj.find(key) != obj.end()) { | ||
| if (holds_alternative<vector<int>>(obj.at(key))) { | ||
| if (holds_alternative<vector<int>>(newValue)) { | ||
| vector<int>& oldVec = get<vector<int>>(newValue); | ||
| const vector<int>& newVec = get<vector<int>>(obj.at(key)); | ||
| oldVec.insert(oldVec.end(), newVec.begin(), newVec.end()); | ||
| sort(oldVec.begin(), oldVec.end()); | ||
| oldVec.erase(unique(oldVec.begin(), oldVec.end()), oldVec.end()); | ||
| } else { | ||
| newValue = obj.at(key); | ||
| } | ||
| } else if (holds_alternative<vector<int>>(newValue)) { | ||
| get<vector<int>>(newValue).push_back(get<int>(obj.at(key))); | ||
| } else { | ||
| newValue = obj.at(key); | ||
| } | ||
| } | ||
| } | ||
| result[key] = newValue; | ||
| } | ||
| return result; | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible in python to use similar object structure as shown in example above?
I'm not sure, but correct me if I'm wrong