JSON: How do I prepend parameter name to each of its values? - javascript

I use the following code to return a JSON string and convert it to a URL.
function runMyReport(){
var json = ko.toJSON(getParams());
var url = "/Report/rvp.aspx?id=" + guestId() + "&" + $.param({ "parameters": json });
}
The problem is that if any parameter has multiple values then it will render as:
{"Product" : "1,2,3"}
and need it to prepend the parameter name to each value and return as:
{"Product" : "1", "Product" : "2", "Product" : "3"}
Can anyone tell me how I can accomplish this?
The reason I need to do this is I created a drop-down field using the below JS array of objects.
var product= [
{ name: "All", id: "1, 2, 3" },
{ name: "key1", id: "1" },
{ name: "key2", id: "2" },
{ name: "key3", id: "3" }
The object "All" has multiple values and when selected displays in the JSON as "Product" : "1,2,3". The problem is that SSRS only accepts multiple values in the following query string format:
querystring.aspx?product=1&product=2&product=3

Related

Mongoose : get content of multiple subdocument in one object

Hello I've been trying to select all the subdocument of multiple document returned in ONE single object and if it's possible sorted by value.
Here is what I mean :
My collection :
{
userId: 1,
...
items : [
{name: "candle", value:5},
{name: "shoes", value: 200}
]
},
{
userId: 2,
...
items : [
{name: "burger", value:17},
{name: "car", value: 5000}
]
},
{
userId: 3,
...
items : [
{name: "chips", value:2},
]
}
And I want to make a query that return me something like that :
[
{userId:1, name: "candle", value:5},
{userId:1, name: "shoes", value: 200},
{userId:2, name: "burger", value:17},
{userId:2, name: "car", value: 5000},
{userId:3, name: "chips", value:2}
]
I've been trying this :
Mymodel.find({}).select("items"); //It return me all the items but in separate object. [ {items: []} ...]
Also if it is possible I would like to sort them : I want to get the 3 most valuable items in the users collection. I tried to use the aggregate method following some example in the mongoose doc but I didn't succeed to make it work. I also have been thinking to make a new database containing only the users items so I can sort them easily but I feel like there is a better solution to do this.
So my question is do you have an idea how could I make it work ? thank you in advance
I'm pasting just an option
var pipeline = [
{
"$unwind" : "$items"
},
{
"$addFields" : {
"name" : "$items.name",
"value" : "$items.value"
}
},
{
"$unset" : "items"
},
{
"$project" : {
"_id" : 0,
"name" : 1,
"value" : 1,
"userId" : 1
}
}
]
Run db.collname.aggregate(pipeline) or review the syntax for the driver.

how to convert json data into a list of values (for linechart)

i have json data in following format.
{
"2" : {
"question" : "How did you like our Food?",
"avg_rating" : {
"2016-05-23" : 3.7142857142857,
"2016-05-25" : 4.5
}
},
"3" : {
"question" : "How did you like our drinks?",
"avg_rating" : {
"2016-05-23" : 3.4285714285714,
"2016-05-25" : 4
}
}
}
i want it to be formatted in following format where above data should be inside data variable of plot object.
var plot = [{
label: "",
data: [
[2016-05-23, 3.7142857142857],[2016-05-25, 4.5]
],
points: {fillColor: '#006699'}
},{
label: "",
data: [
[2016-05-23, 3.4285714285714],[2016-05-25, 4]
],
points: {fillColor: '#009933'}
}];
i am not good in english so i may not have been able to explain this problem properly. please help me to sort out this problem.

How to create following type of json that will usefull in type ahead with angular js

I have successfully implement type ahead using angular js using this codepen.
Now my data is dynamic so I want to create my own json that should looks something like following:
$scope.states = [
{
name: "ABCD",
id: "AB"
},
{
name: "XYZ",
id: "XY"
},
{
name: "OPQR",
id: "OP"
},
{
name: "LMNO",
id: "LM"
},
{
name: "KLM",
id: "KL"
}
];
How to create like above json and store in $scope variable so I can access it using name and id separately.
Please help me...!!
I am giving by my own now cause I got it little bit late.
Solution is I have pushed the data wrong I have solved it like:
$scope.states.push({
name: "string",
id: int,
});
and I got it.

CanJS Model findAll returns list of duplicate items

I'm using can.Model to retrieve data by id:
Invoice = can.Model({
findAll: 'GET /invoices',
create : "POST /invoices",
update : "PUT /invoices/{id}",
destroy : "DELETE /invoices/{id}"
},{});
When navigating to /invoices, the result is as expected, like:
[
0: {
"ID": "1",
"Client": "Client1",
},
1: {
"ID": "2",
"Client": "Client2"
}
]
However, the data retrieved with Invoice.findAll and logged to the console, it looks like this, with the same data item repeated for each element in the list:
[
0: {
"ID": "1",
"Client": "Client1"
},
1: {
"ID": "1",
"Client": "Client1"
}
]
The response from the server is correct, so why is it being interpreted as a list of identical items?
When data has an id field different from id, it should be specified using the id field. For instance, as mentioned in the documentation for can.Model in .NET it is common to use Id. In this case, the id field must be defined as ID:
Invoice = can.Model({
id: 'ID',
findAll: 'GET /invoices',
create : "POST /invoices",
update : "PUT /invoices/{id}",
destroy : "DELETE /invoices/{id}"
},{});

How to access JSON element which has "-" character in the name in JavaScript

I have a JSON object which returns following values.
{
"articles":[
{
"paper-id":"id",
"paper-id-type":"type",
"title":"title",
"url":"url",
"abstract":"abs",
"date":"date",
"publication-forum":"forum",
"publication-forum-type":"type",
"authors":"auth",
"keywords":"key1,key2"
}
}
I tried to access these results through JavaScript.
First I created an array and assigned these results to the array.
The content of the array (named articles) object looks like this;
abstract: "xxx"
authors: "yyy"
date: "1111"
keywords: "key1, key2"
paper-id: "abc"
paper-id-type: "xxx"
publication-forum: "yyy"
publication-forum-type: "zzz"
title: "www"
url: "url"
Then I tried to access each value in these elements using the format,
articles[0]["abstract"]
It works for elements that do not have "-" character. So when I tried to extract the paper-id;
articles[0]["paper-id"]
I'm getting the error [Exception: SyntaxError: Unexpected token []
Does anyone know how to solve this problem ?
The problem is because you forgot to close the [] and the {} in your JSON
You JSON should look like this
{
"articles":[
{
"paper-id":"id",
"paper-id-type":"type",
"title":"title",
"url":"url",
"abstract":"abs",
"date":"date",
"publication-forum":"forum",
"publication-forum-type":"type",
"authors":"auth",
"keywords":"key1,key2"
}]
}
abc = {
"articles": [{
"paper-id": "id",
"paper-id-type": "type",
"title": "title",
"url": "url",
"abstract": "abs",
"date": "date",
"publication-forum": "forum",
"publication-forum-type": "type",
"authors": "auth",
"keywords": "key1,key2"
}
]
};
for (i in abc['articles'][0]) {
console.log(abc['articles'][0][i]);
}

Categories