I get this error that I don't understand:
`missing ; before statement`
Here is the code
},
'addevent' : function(data, formData){
var entry = fx.deserialize(formData);
},
'deserialize' : function(str){
var data = str.split("&"),
pairs = [], entry{}, key, val; //ERROR
The syntax
entry{}
doesn't look right to me.
Do you want
entry={},
Have it in one line:
var data = str.split("&"), pairs = [], entry{}, key, val;
You are missing = sign, should be:
..., entry = {}, ...
},
'addevent' : function(data, formData){
var entry = fx.deserialize(formData);
},
'deserialize' : function(str){
var data = str.split("&");
pairs = [];
entry = {key, val};
var data = str.split("&"), <====== Here should be ';'
!var! pairs = [], entry{}, key, val; //ERROR // var should be again
Related
I have an array which is initialized with
var detail = [];
// Have also tried var detail = new Array;
Through the rest of my code, I loop through a data set creating an object in the following manner
while(true) {
var tempObj = {
qty : "",
size : "",
adr : "",
dir : ""
}
// Some Logic
tempObj.qty = val;
tempObj.size = val;
tempObj.adr = val;
tempObj.dir = val;
detail.push(tempObj);
}
Then when I attempt to JSON.Stringify my detail array to send through an AJAX request, it returns a blank array []. This can also be seen in the below screenshot of the console.
Note: The indent at the bottom calling out the prototype as an object is because the screenshot is from the object I am attempting to send to my server. There is other non array based data in that object. I want to stringify the array so it properly sends to the server.
What is causing this behavior and how can I correct it?
Edit 1: Execution of JSON.stringify(detail)
var requestObj = {
field1: “a”,
field2: “b”,
field3: “c”,
data: JSON.stringify(detail)
}
Edit 2: Add full block of code
$("#submit-button").click(function() {
var reqDate = $("#reqDate").val();
var wonum = $("#wonum").val();
var svc = $("#svc").val();
var complaint = $("#complaint").val();
var comments = $("#comm").val();
var detail = new Array;
var topValid = false;
$(".tb-rw").each(function() {
var qty = $(this).find(".quantity");
var size = $(this).find(".size");
var adr = $(this).find(".address");
var dir = $(this).find(".direction");
var mgk = $(this).find(".mgKey");
var tempObj = {};
if(fail == true || topValid == true) {
alert("Please Make Sure all Inputs are Filled Out");
return;
} else {
//tempArr.push(qty.val(), size.val(), dir.val());
tempObj.qty = qty.val();
tempObj.size = size.val();
tempObj.dir = dir.val();
}
findSimilarJobs(adr.val(), mgk.val(), function(data) {
if(data != "clear") {
console.log("FAILED VALIDATION");
console.log(data);
adr.css('border-color', 'red');
alert("Please Make Sure all Addresses are Valid");
return;
} else {
//tempArr.push(adr.val());
tempObj.adr = adr.val();
}
detail.push(tempObj);
});
});
console.log("Preparing to send!");
var requestData = {
"requestedDate": reqDate,
"svc": svc,
"wonum": wonum,
"complaint": complaint,
"comment": comments,
"data": JSON.stringify(detail)
};
console.log(requestData);
console.log(JSON.stringify(detail));
});
Stringify should be all lower case like this stringify, otherwise is won't work. So like, this JSON.Stringify(detail) should be this JSON.stringify(detail)
your tempObj is JSON object and it should have key:value like below
while(true) {
var tempObj = {
qty : "",
size : "",
adr : "",
dir : ""
}
// or you can create empty object
// var tempObj = {};
tempObj.qty = val;
tempObj.size = val;
tempObj.adr = val;
tempObj.dir = val;
detail.push(tempObj);
}
I want to save variable 'beauftragungsdatum' type 'Date' to engine :
inject(['$http', 'Uri', function($http, Uri) {
camForm.on('store', function(evt) {
evt.retrieveVariables()
var varManager = evt.variableManager;
var vars = varManager.variables;
var dataVar = $scope.beauftragungsdatum;
varManager.variableValue('beauftragungsdatum', dataVar);
var variableData = {};
for(var v in vars) {
if(varManager.isDirty(v)) {
var val = vars[v].value;
if(varManager.isJsonVariable(v)) {
val = JSON.stringify(val);
}
variableData[v] = {
value: val,
type: vars[v].type,
valueInfo: vars[v].valueInfo
};
}
}
var data = { modifications: variableData };
var config = {
headers : {
'Content-Type': 'application/json'
}
};
$http.post(Uri.appUri('engine://engine/:engine/task/' + camForm.taskId + '/variables'), data, config);
evt.storePrevented = true;
});
}]);
At the save moment I have a problem : variable 'beauftragungsdatum' is not type of date. I found out that var have wrong format. I fixed it and paste some decision:
inject(['$http', 'Uri', function($http, Uri) {
camForm.on('store', function(evt) {
evt.retrieveVariables()
var varManager = evt.variableManager;
var vars = varManager.variables;
var now=moment(dataVar).format("YYYY-MM-DDTHH:mm:ss.sssZZ");
console.log(now);
console.log(typeof now);
var d=varManager.variableValue('beauftragungsdatum');
console.log(d);
console.log(typeof d);
varManager.variableValue('beauftragungsdatum')=now;
var variableData = {};
for(var v in vars) {
if(varManager.isDirty(v)) {
var val = vars[v].value;
if(varManager.isJsonVariable(v)) {
val = JSON.stringify(val);
}
variableData[v] = {
value: val,
type: vars[v].type,
valueInfo: vars[v].valueInfo
};
}
}
var data = { modifications: variableData };
var config = {
headers : {
'Content-Type': 'application/json'
}
};
$http.post(Uri.appUri('engine://engine/:engine/task/' + camForm.taskId + '/variables'), data, config);
evt.storePrevented = true;
});
}]);
As a result i have a problem attach file.
enter image description here
I need help in this question , may be know how save var 'Date' in Camunda 7.9.
In the following line, the value you are placing inside now is a string:
var now=moment(dataVar).format("YYYY-MM-DDTHH:mm:ss.sssZZ");
The format method returns a string in the format you specify.
if you want to get a Date object, use moment(dataVar).toDate().
I have one json file with following structure:
{
"won":3,
"lost":0,
"void":0,
"active":1
}
I've been trying to figure out how to make two arrays in javascript that will contain following information:
var labels = ["won", "lost", "void", "active"];
var data = ["3","0", "0","1"];
But I can't my head around how to do it.
You can use:
var labels = Object.keys(input);
var data = Object.values(input);
However you should check the browsers compatibility:
Object.keys
Object.values
Try this code
var item_list = {
"won":3,
"lost":0,
"void":0,
"active":1
};
var labels = [];
var data = [];
for(var item in item_list){
// console.log(item);
labels.push(item);
data.push(item_list[item]);
}
console.log(labels);
console.log(data);
You can do this too
var mydata = {
"won":3,
"lost":0,
"void":0,
"active":1
};
var first = [],
second = [];
for (var property in mydata) {
if ( ! mydata.hasOwnProperty(property)) {
continue;
}
first.push(property);
second.push(mydata[property]);
}
console.log(first);
console.log(second);
Try This :
var obj = {
"won":3,
"lost":0,
"void":0,
"active":1
}
var keys = new Array();
var values = new Array();
for (var key in obj) {
keys.push(key)
values.push(obj[key]);
}
console.log(keys);
console.log(values);
I have a function which updates the URL query string. in some scenarios it works fine where i have single key and array of values but when i have multiple keys with array of values it doesn't append it to query string but if I see the object i can see the keys and its value. Following is my function
function Querystring(obj) {
var querystring = "";
// Build the query string
var currentQueryString = parseQueryString();
var newQueryString = "";
var dataCategory = Object.keys(obj)[0];
console.log(dataCategory);
// Set the new value
if (obj[dataCategory] != null) {
currentQueryString[dataCategory] = obj[dataCategory].join(",");
// Loop the keys in currentQueryString and contruct new querystring
Object.keys(currentQueryString).forEach(function (key, index) {
if (index == 0) {
newQueryString += "?" + key + "=" + currentQueryString[key];
} else {
newQueryString += "&" + key + "=" + currentQueryString[key];
}
});
}
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + newQueryString;
window.history.pushState({ path: newurl }, '', newurl);
return newQueryString;
}
This is how i am calling this function
$('.select').change(function () {
var SortOrder = $(this).attr("id");
var SortBy = $(this).data("sortby");
var obj = {};
obj[SortOrder] = $(this).val();
obj[SortBy] = [$('option:selected', this).data("price")];
Querystring(obj);
});
Thanks in advance
You can simply this quite a bit. Try the example below. Ignore the getParser function as it is there just for us to be able to test this code.
function getParser(url) {
var a = document.createElement("a");
a.href = url;
return a;
}
function getUpdatedURL(url, newQueryParams) {
var searchParams = url.search.substring(1).split("&");
var obj = {};
searchParams.forEach(function(param){
var paramParts = param.split("=");
obj[paramParts[0]] = paramParts[1];
});
obj = Object.assign(obj, newQueryParams);
url.search = "?"+Object.keys(obj).map(p => p+"="+obj[p]).join("&");
return url.href;
}
var url = getParser("http://example.com/test/page?blah=abc&test=123");
//Ignore this as this is just for us to be able to test and run this example. You'll pass in the window.location instead of the url variable defined here.
console.log(getUpdatedURL(url, { sortOrder: "1345", sortBy: ["price","other","more"] }));
Writing code for it is quiet easy, but to get the full juices, i recommend using this tiny library:
https://www.npmjs.com/package/query-string
i wrote my own code to build and destruct a query string, but i switched to this one as it gives the full rainbow.
Usage
const queryString = require('query-string');
console.log(location.search);
//=> '?foo=bar'
const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}
console.log(location.hash);
//=> '#token=bada55cafe'
const parsedHash = queryString.parse(location.hash);
console.log(parsedHash);
//=> {token: 'bada55cafe'}
parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'
location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'
I would like to get the length of the 'obj' object so that I can do a for-loop with it, here is my code:
var oRequest = new XMLHttpRequest();
var sURL = "/Users/files/Documents/time.json";
oRequest.open("GET",sURL,false);
oRequest.send();
var txt = oRequest.responseText;
var obj = eval ("(" + txt + ")");
My goal is that I would like to output everything in this time.json file to the screen.
Here is what my json file looks like:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
The object you create does not have have a length. I assume you meant employees length and not the obj length
Like this:
Live Demo
var txt = oRequest.responseText;
// eval("var obj = "+txt); // eval is not recommended but here is how
var obj = JSON.parse(txt); // this is better
var emp = obj?obj.employees:[]; // if we have a useful object, get the employees
window.onload=function() {
var empDiv = document.getElementById("empDiv");
for (var i=0;i<emp.length;i++) {
empDiv.innerHTML+=emp[i].firstName + " "+ emp[i].lastName + '<br/>';
}
}
you don't have to get a length in order to do a for loop on an object, based on your object structure you can get data with the following loop:
for(var key in object) {
var o = object[key];
for(var i = 0; i < o.length; i++) {
var user = o[i];
alert(user.firstName);
}
}