So here's a question. What I want to do is generate a data structure given a set of input values.
Since this is a multiple language submission, let's consider the input list to be an array of key/value pairs. And therefore an array of Hash, Map, Dictionary or whatever term that floats your boat. I'll keep all the notation here as JSON, hoping that's universal enough to translate / decode.
So for input, let's say we have this:
[ { "4": 10 }, { "7": 9 }, { "90": 7 }, { "1": 8 } ]
Maybe a little redundant, but lets stick with that.
So from that input, I want to get to this structure. I'm giving a whole structure, but the important part is what gets returned for the value under "weight":
[
{ "$project": {
"user_id": 1,
"content": 1,
"date": 1,
"weight": { "$cond": [
{ "$eq": ["$user_id": 4] },
10,
{ "$cond": [
{ "$eq": ["$user_id": 7] },
9,
{ "$cond": [
{ "$eq": ["$user_id": 90] },
7,
{ "$cond": [
{ "$eq": ["$user_id": 1] },
8,
0
]}
]}
]}
]}
}}
]
So the solution I'm looking for populates the structure content for "weight" as shown in the structure by using the input as shown.
Yes the values that look like numbers in the structure must be numbers and not strings, so whatever the language implementation, the JSON encoded version must look exactly the same.
Alternately, give me a better approach to get to the same result of assigning the weight values based on the matching user_id
.
Does anyone have an approach to this?
Would be happy with any language implementation as I think it is fair to just see how the structure can be created.
I'll try to add myself, but kudos goes to the good implementations.
Happy coding.
See Question&Answers more detail:
os