I have an input json as:
{
"setInfo":{
"userset1":{
"details1":[
"somedata1",
"somedata2"
],
"details2":[
"somedata3",
"somedata4"
]
},
"userset2":{
"details3":[
"somedata5",
"somedata6"
],
"details4":[
"somedata7",
"somedata8"
]
}
}
}
I want to convert this into below output json:
{
"setInfo":{
"userset1":{
"details1":{
"options":[
"op1",
"op2",
"op3"
]
},
"details2":{
"options":[
"op1",
"op3"
]
}
},
"userset2":{
"details3":{
"options":[
"op1"
]
},
"details4":{
"options":[
"op4"
]
}
}
}
}
In the above input json somedata1, somedata2...somedata8 are variables that I need to pass to a service endpoint that would return me corresponding options for details1.....details4 which are op1,op2 etc
So I want to loop through the above input json and update the above with values returned from my service.
So I tried with the below code.
//my inital service call to get data
myservice.getData().then(function(result){
// Below is console output from result.setInfo
// setInfo:
// userset1:
// details1: (2) ["somedata1", "somedata2"]
// details2: (2) ["somedata3", "somedata4"].
// userset2:
// details3: (2) ["somedata5", "somedata6"]
// details4: (2) ["somedata7", "somedata8"]
//Now I tried looping through the json and update but not getting through as I feel something is incorrect below
_.each(result.setInfo, function (set, name) {
$scope.sets[name] = {};
_.each(set, function (data1, name1) {
//This is the service which gets corresponding options op1,op2 etc according to the params that were passed
myservice.getArrayData({ data: data1}).function((res){
//this is where I feel something is not correct. .pluck gets the correponding data fine but its the assignment which is not correct.
$scope.sets[name].setInfo[name].options = _.pluck(res, 'name');
});
});
});
});
With the above code I get the below json which is not what I expect,
{
"setInfo":{
"userset1":{
"options":[
"op1",
"op2"
]
},
"userset2":{
"options":[
"op1",
"op2"
]
}
}
}
So see how it removed some tags above.
Could anyone point me to whats missing.
Sorry for long post but wanted to cover as much as I can.
--Updated--
So I updated to use this code
$scope.setInfo[name][name1] = _.pluck(jobs, 'name');
And with this I made further progress that now my output json became as:
{
"setInfo":{
"userset1":{
"details1":[
"op1",
"op2",
"op3"
],
"details2":[
"op1",
"op3"
]
},
"userset2":{
"details3":[
"op1"
],
"details4":[
"op4"
]
}
}
}
So the only thing missing here is how to add the options tag.
I tried with this
$scope.setInfo[name][name1].options = _.pluck(jobs, 'name');
But this gives me error:
Cannot set property 'options' of undefined
Related
I have an app where users can log in and add trips they've been on. my fb db structure is like so
{
"users": {
"randomId": {
"Place1": {
"name": "Place1",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
}
]
}
}
}
}
you can see the key above I want to update. but my problem is, I first need to check if Place1 already exists in the database? how do I achieve this? I know I could call set but not sure if this is the best thing to do? I'm also aware of update but I want logic like this pseudo code below
if (place1) {
// call update()
} else {
// cat set()
}
I'm not sure how to do the if(place1) part though? and if it's best practice in firebase to check if it exists?
You could try use Object.keys(obj) to get attribute of object.
assume that userObj is
"users": {
"randomId": {
"Place1": {
"name": "Place1",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
},
]
},
}
}
and you have mentioned that it could be mulitple PlaceN under randomID, then I suggest that the pattern might be :
multiple case, have Place2 and value of Place1 is null:
"randomId": {
"Place1": {
},
"Place2": {
"name": "Place2",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
},
]
},
}
and the check function:
checkPlace1ExistOrNot=(userObj)=>{
let tempRandomId = userObj[Object.keys(userObj)[0]] //It should be vale of "RandomId"
if(tempRandomId[Object.keys(tempRandomId)[0]])
{
return true // users.randomId.Place1 is exist
}
else
{
return false // users.randomId.Place1 is not exist
}
}
Hi I have the following data but cannot get DataTables to display it:
const data = [{
"School":"Arsenal 2011",
"Group":{
"Name":"Previous",
"ParentGroup":{
"Name":"Arsenal",
"ParentGroup":{
"Name":"USA",
"ParentGroup":null
}
}
},
"GroupDisplayText":null,
"Publisher":"Abbot",
"PublishedDate":"2011",
"PublishersWebsite":"http://google.com/USA/ADW%202011/Arsenal%202011.pdf"
},
{
"School":"New York 2000",
"Group":{
"Name":"New York",
"ParentGroup":{
"Name":"USA",
"ParentGroup":null
}
},
"GroupDisplayText":null,
"Publisher":"DoE",
"PublishedDate":"2000",
"PublishersWebsite":"http://google.com/USA/New York%202000%20Tables.pdf"
}];
$(document).ready(function () {
$('#example').DataTable( {
"ajax": data
} );
}
I have created a project on playcode
https://playcode.io/470603
I am getting a datatables error
DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
I have checked the json and all is ok?
Thanks
$(document).ready(function () {
$('#example').DataTable( {
data:data // ajax for to make ajax request to retrieve data.
"columns": [ // also needs to specify column config to display it
{ "data": "School" },
]
} );
}
for javascript object use data instead.
I want to add a new object for each nested array. I'm calling this function any time I add a product to my orderintake:
add2order(productID, productName, productRatePlans) {
this.orderIntake.push({ productID, productName, productRatePlans });
let i = 0;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
i++;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].defaultQuantity;
});
}
this is an example response from the server:
{
"id": "8adc8f996928b9a4016929c59b943a8f",
"sku": "SKU-00006778",
"Partner_Account_ID__c": null,
"productRatePlans": [
{
"id": "8adce4216928c28d016929c59bff3372",
"status": "Active",
"name": "Enterprise",
"description": null,
"effectiveStartDate": "2016-02-26",
"effectiveEndDate": "2029-02-26",
"productRatePlanCharges": [
{
"id": "8adc8f996928b9a4016929c59d183a92",
"name": "USAGE_COUNTER_2",
"type": "Usage",
"model": "Volume",
"uom": "Each",
"pricingSummary": [
"Up to 5000 Each: USD0 flat fee"
],
"pricing": [
{
...
}
],
"defaultQuantity": null,
"applyDiscountTo": null,
"discountLevel": null,
"discountClass": null,
...
"financeInformation": {
..,
}
}
]
}
],
"productFeatures": [
{
...
}
]
}
The data is being retrived this way from an external REST backend so unfortunately I can't initialize the data including the new property...
so in every productRatePlanCharges there should be 1 new object 'quantity'.
How can I add this field to every productRatePlanCharges?
Right now I'm getting: ERROR
TypeError: Cannot read property 'productRatePlanCharges' of undefined
And how can I make sure I'm always adding this to the last orderIntake element? Don't mind productRatePlans there is only 1 in each orderintake...
thanks for your support!
Here you have to create productDetails object with inititalised array like below so that you won't get the error.
add2order(productID, productName, productRatePlans) {
// Create object like below
let productDetails = { productID : productID, productName : productName, productRatePlans : productRatePlans
}
this.orderIntake.push(productDetails);
let i = 0;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges.forEach(element => {
i++;
this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].quantity = this.orderIntake[0].productRatePlans[0].productRatePlanCharges[
i
].defaultQuantity;
});
}
Hope this will help!
as you used Angular you probably use Typescript too. I recommend that you create a model like your incoming model and there define your quantity: number inside productRatePlanCharges object. then map the incoming data to your own model. therefore you will have a quantity=0 in your model that you can change it later in a loop.
If you want to continue with your own way take a look at this:
Add new attribute (element) to JSON object using JavaScript
there is no problem to add an element to current model almost like you did, and the problem might be somewhere else as your error refers to existence of productRatePlanCharges!
as you used forEach I prefer to use that 'element' and double iterating with i++; is not a good idea to me.
this might be better:
element.quantity = element.defaultQuantity;
I am sure I must have done something stupid, but I can't figure this one out.
Working in Google Apps Script, I have an object something like this:
{
"advertiserId":"123456",
"advertiserName":"Name",
"advertiserVariables":{
"U5":{
"reportName":"transaction_date",
"dataType":"STRING"
},
"U6":{
"reportName":"Page_URL",
"dataType":"STRING"
},
"U7":{
"reportName":"Custom_1",
"dataType":"STRING"
},
"U8":{
"reportName":"Custom_2",
"dataType":"STRING"
},
"U9":{
"reportName":"Custom_3",
"dataType":"STRING"
},
"U1":{
"reportName":"pageType",
"dataType":"NUMBER"
},
"U2":{
"reportName":"productName",
"dataType":"STRING"
},
"U3":{
"reportName":"productId",
"dataType":"NUMBER"
},
"U4":{
"reportName":"productCategory",
"dataType":"STRING"
}
},
"audienceSets":{
"4098242":{
"tagName":"Name - Tag",
"audienceSetName":"Visitors",
"audienceGroups":{
"productCategory":{
"variableType":"U4",
"audienceGroupName":"productCategory",
"dataType":"STRING",
"values":["category1","category2"],
"uniqueValues":2,
"active":true,
"recency":30
}
}
}
},
"shortAdvertiserName":"Name"
}
and in a function I want to loop over each tagId in audienceSets:
function buildRemarketingList(parameters) {
initProfileId();
Logger.log(parameters);
for (var tagId in parameters.audienceSets) {
Logger.log(tagId);
Logger.log(parameters.audienceSets[tagId]);
}
}
The first log action is returning the object as expected, with multiple objects within audienceSets. Logging the first value of tag also works as expected. However, I then get undefined for the line parameters.audienceSets[tagId] and the second loop doesn't work.
Since the values of tagId are being generated directly from the object, I don't understand how this can be going wrong?
i have json response like this :
{
"gasal": [
{
"prodi": "Teknik Industri",
"jumlah": "3.5000"
},
{
"prodi": "Teknik Informatika",
"jumlah": "6.0000"
}
],
"genap": [
{
"prodi": "Teknik Informatika",
"jumlah": "2.0000"
}
],
"prodi": [
{
"nama_prod": "Teknik Informatika"
},
{
"nama_prod": "Teknik Industri"
}
]}
i want to show the response to bar chart (i use chart js), but the problem is i dont know how to call the json response in javascript.
this is my javascript code :
var url = "{{url('test')}}";
var Prodi = new Array();
var gasal = new Array();
var genap = new Array();
$.get(url, function(response){
response.forEach(function(data){
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
});
})
i think the problem is in this code
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
what is the right code ?
thank you.
Gasal.push(data.genap[0]) would be the whole object
Gasal.push(data.genap[0].jumlah) would be the property of the object
Because you have created an array with an object inside and then you need to access the properties of the object.