1

I am trying to convert my JSON result to an array to bind it to my Kendo controls. The JSON result that I am getting is as follows.

"Issues": [
    {
        "Id": null,
        "Key": null,
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Rya"
            },
            {
                "Key": "UserName",
                "Value": "RH"
            },
            {
                "Key": "Count",
                "Value": "350"
            }
        ]
    },
    {
        "Id": null,
        "Key": null,
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Mike"
            },
            {
                "Key": "UserName",
                "Value": "ML"
            },
            {
                "Key": "Count",
                "Value": "90"
            }
        ]
    }
]

The array that I needed to bind it to the Kendo control is as below.

 { "Display Name": 'Rya', "UserName" : "RH", value: 350 },
 { "Display Name": 'Mike', "UserName" : "ML", value: 90 }

i)I dont want to hardcode the strings "Display Name", "User Name", "RH". I tried v.Values[0].Key: v.Values[0].Value, but it didn't work.

ii) Also I will not know how many "key, value" pairs will be present, so I need to loop through the Values and generate the array instead of fixed

category: v.Values[0].Value, 
UserName: v.Values[1].Value,
  value: v.Values[2].Value,
.
.
.
score: v.values[n].value
3
  • provide a sample of what this new array would look like Commented Mar 1, 2017 at 21:28
  • What have you tried so far? Edit to include code and a proper language tag. Commented Mar 1, 2017 at 21:28
  • 1
    @JonathanPortorreal The new array should look like { category: 'Rya', value: 350 }, { category: 'Mike', value: 90 } Commented Mar 1, 2017 at 22:03

1 Answer 1

3

If you're using ES6, you don't really need lodash in this case:

var r = json.Issues.map(v => ({ 
  category: v.Values[0].Value, 
  value: v.Values[2].Value,
}));

http://codepen.io/cjke/pen/RprJdG?editors=0010

If want to use lodash or not-ES6, then:

var r = _.map(json.Issues, function(v) { 
  return {
    category: v.Values[0].Value, 
    value: v.Values[2].Value,
  }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I am trying to develop this in Angular2 using typescript.
If it worked make sure to mark it as correct so future readers of the question/answer pair can see what worked.
Hi Chris, a quick question. Here instead of category and value, I need "Display Name" and "Count". Also I don't want to hardcode the items here. Because my json can have any number of fields. So basically like this - { "Display Name": 'Rya', "User Name" : "RH", "Count": 350 ....... }, but dynamically because there might be a 4th field added.
I'm not sure I understand the question - can you post a new question so it can be understood? Alternatively, perhaps update the question with an "edit" and the new content below
Hi Chris, Did you get a chance to look in to it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.