How to add each jquery array values inside the single quotes - javascript

I have a array in jquery.Now as per my need i have to add each array values into single quotes as ..
var toc='INCOMING','INETCALL','ISD','LOCAL','STD'
But at present i have values like this ..
var toc=INCOMING,INETCALL,ISD,LOCAL,STD
And here is my codes ..
$.ajax({
type: 'GET',
url: 'getdata',
async:false,
dataType: "text",
success: function(data) {
var values = [];
values = data;
values=values.replace('[','');
values=values.replace(']','');
var array = values.split(",");
for(var i=0,len=array.length;i<len;i++)
{
if($.isNumeric(array[i]))
{
callcost.push(array[i]);
}
else
{
toc.push(array[i]);
}
}
alert(toc);
alert(callcost);
}
});

not sure if i got your question right but i guess you are messing up with all this replace/split/... logic. If the data object is an array just try this
$.ajax({
type: 'GET',
url: 'getdata',
async:false,
dataType: "text",
success: function(data) {
var array = JSON.parse(data);
$.each(array, function(i, val){
if($.isNumeric(val)) {
callcost.push(val);
}else{
toc.push(val);
}
});
}
});

Related

Ajax Call not storing data in Array

The following code is not storing values in the Array
var checkListIdForEmail= new Array();
var checkListNameforEmail;
function getCheckListIdAndName() {
$.ajax({
type: "GET",
url: 'URL/' + 12464,
dataType: 'json',
contentType: false,
processData: false,
success: function (result) {
for (var i=0; i< result.length;i++) {
$('#checkListIdForEmail').val(result.checklistDetailId[i]);
}
// alert("Success");
},
error: function (error) {
alert("Errror while getting header values");
}
});
}
Can anyone please let me know what needs to store all data in an array..
Thank You
I would suggest on your success callback, do this instead.
success: function (result) {
checkListIdForEmail = result;
},
since result is already an array
Maybe this is what you want to store to the checkListIdForEmail array:
for (var i=0; i< result.length;i++) {
checkListIdForEmail[i] = result[i].checklistDetailId;
}
$('#checkListIdForEmail').val(checkListIdForEmail);

How do i split ajax return values using jquery

I want to split the ajax returned values using jQuery.
Here is my code:
var url = "/StudentProgress/GetStudProgDet/";
$.ajax({
url: url,
data: { currentAcadYr: iAcademicYearText, currentSem: iSemesterText },
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (data) {
var result = $(data).text().split(':');
var ProgAcadYearCode = result[0].ProgAcadYearCode;
var productSize = result[1];
// alert(data.ProgAcadYearCode);
//$("#ToProgressAcademicYearId option").filter(function () {
// return this.text == testsem;
//}).attr('selected', true);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
I got a result like this:
data = {
success: true,
progAcadYearCode: 20172018,
progAcadYearId: 17,
progressSemId: 47,
progressSemNo: 2
}
How do I extract the desired values from the JSON using jQuery?
Based on data what you shown,you have to directly fetch it's properties like below:-
success: function (data) {
console.log(data.success);
console.log(data.progAcadYearCode); //and so on
},

Adding to my list array from my JSON object

This is what I am currently doing. I tried just pushing the values from my json into the links array, but upon entering the array the values become undefined. What is a better way to move the data and have it still be usable??
var links = [];
$.ajax({
url: 'data.json',
dataType: 'json',
type: 'get',
cache: false,
success: function(data) {
$(data.emails).each(function(index, value) {
links.push(value);
});
}
});
//data.json contains:
{
"emails": [{
"source": "1.11913372.-2#multexinvestornetwork.com",
"target": "pallen#enron.com"
}]
}
result from using this code is the same. The values enter the array, but when you access them via links.(name of value) it returns 'undefined'
$.each(data.emails, function(index, value) {
links.push(value);
console.log(links);
return value;
});
Result
Try This
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
//alert("data");
var links = [];
$.ajax({
url: 'test1.php',
type: 'post',
dataType: 'json',
data: {
}
}).done(function(data) {
$.each(data.emails, function(index, value) {
links.push(value);
console.log(links);
return value;
});
});
function getVal(){
console.log(links[0].target);
}
</script>
<button onclick="getVal()">Get target</button>
I think your success callback code is wrong,
the correct way to loop data.emails is,
success: function(data) {
$.each(data.emails, function(index, value) { // <--- change this
links.push(value);
console.log(links); //<--- updated
return value;
});
}
You're calling console.log on a key that doesn't exist. Modify your code to call and print out the array, not a random property.
var links = [];
$.ajax({
url: 'data.json',
dataType: 'json',
type: 'get',
cache: false,
success: function(data) {
$(data.emails).each(function(index, value) {
links.push(value);
console.log(links);
return value;
});
}
});

how to send whole array to servlet and how to access it

I am trying to get all jqgrid data in one array and sending to servelet,so far I am try with this-
var rows= jQuery("#list").jqGrid('getRowData');
var paras=new Array();
for(var i=0;i<rows.length;i++)
{
var row=rows[i];
paras.push($.param(row));
//alert(paras[i]);
}
alert(paras);
$.ajax({
type: "POST",
url: "JQGridServlet?action=arraydata&paras="+paras,
data: paras.join('and'),
success: function(msg)
{
alert(msg);
}
});
but it send only first 'srno'.not whole array.
please any body suggest me to how to send array to servlet and how to access it on servlet.
Try this:
var griddata= $( "#list" ).getRowData();
var model = {
grid: griddata
};
var paras= JSON.stringify( model );
alert(paras);
$.ajax({
type: "POST",
url: "JQGridServlet?action=arraydata&paras="+paras,
data: paras.join('and'),
success: function(msg)
{
alert(msg);
}
});

looping through JSON array

I have recently posted another question which straight away users pointed me in the right direction.
$.ajax({
type: 'POST',
url: './',
data: 'token=' + token + '&re=8',
cache: false,
timeout: 5000,
success: function(html) {
auth(html);
var JSON_array = eval(html);
alert(JSON_array[0].username);
}
});
this returns the data correctly but I want to perform a kind of 'foreach'. the array contains data about multiple incoming and outgoing Instant Messages. So if a user is talking to more than one person at a time i need to loop through. the array's structure is as follows.
Array(
[0] => Array
(
[username] => Emmalene
[contents] =>
<ul><li class="name">ACTwebDesigns</li><li class="speech">helllllllo</li></ul>
<ul><li class="name">ACTwebDesigns</li><li class="speech">sds</li></ul>
<ul><li class="name">ACTwebDesigns</li><li class="speech">Sponge</li><li class="speech">dick</li></ul>
<ul><li class="name">ACTwebDesigns</li><li class="speech">arghh</li></ul>
)
)
Any help very much appreciated.
Well since you are using jQuery already you could use the each function:
$.ajax({
type: 'POST', url: './', data: 'token=' + token + '&re=8', cache: false, timeout: 5000,
success: function(html){
auth(html);
var JSON_array = eval(html);
$.each(JSON_array, function(index, data) {
$('someelement').append(data.contents);
});
}
});
Instead of evaluating the HTML, you can even specify JSON as return type...
Iteration is easy when using $.each:
$.ajax({
type: "POST",
data: ...,
url: url,
dataType: "json",
success: function(data) {
$.each(data, function(i, item){
// do something with every item in data
// you can reference items in data via
// data.fieldName
});
}
});
But a for ... in loop isn't much harder:
$.ajax({
...,
dataType: "json",
success: function(data) {
var fields = data.fieldName;
var value;
for (value in fields) {
// do something with value
}
}
});
Just to clarify, As I've read many helpful hints and answers and only this one worked for me:
$.ajax({
type: 'POST', url: './', data: 'token=' + token + '&re=8', cache: false, timeout: 5000, datatype: 'json',
success: function(html){
auth(html);
var JSON_array = eval(html);
$.each(JSON_array, function(index, data) {
var talk_to = JSON_array.username;
var contents_to_update = JSON_array.contents;
});
}
});
this which made work:
1) use of eval.
2) datatype: 'json'
3) use of jquery's $.each function

Categories