I have the following json structure (I've made it shorter for simplify):
{
"employees":{"0":"Name1 Surname1", "1":"Name2 Surname2"},
"managers":{"0":"Name3 Surname3", "1":"Name4 Surname4"},
"teamleaders":{"0":"Name5 Surname5", "1":"Name6 Surname6"},
}
How can I loop ONLY through employees with JQuery? The below code gives me the whole 3 objects and I can't seem to be able to sort them according to the object name:
$.each(item, function (i, item) {
$.each(item, function (key, value) {
//gives me ALL names and surnames
console.log(value);
})
});
You can use plain JS for this.
for (var employee in json.employees) {
if (json.employees.hasownProperty(employee))
console.log(json.employees[employee])
}
Instead of iterating the outer object, item, do like this.
var item = {
"employees":{"0":"Name1 Surname1", "1":"Name2 Surname2"},
"managers":{"0":"Name3 Surname3", "1":"Name4 Surname4"},
"teamleaders":{"0":"Name5 Surname5", "1":"Name6 Surname6"},
}
$.each(item.employees, function(key, value) { ... });
Related
Hello I have a json data like this
KLOBS {"SL_VEF_APPLIED_VS_BOL_R1_KLOBS":["0.00247320692497939","0.000008129750823137272"]}
KL15 {"SL_VEF_APPLIED_VS_BOL_R1_KL15":["0.01890831252229754","0.9008162336184189"]}
I'm already try extract json
$.get('url_request.php?' + $(this).serialize(), function(data) {
var obj = data;
$.each(obj, function(key, value) {
console.log(key);
console.log(value);
});
});
$.each(obj, function(key, value) {
console.log(key);
console.log(value);
});
And I get result
KLOBS
{"SL_VEF_APPLIED_VS_BOL_R1_KLOBS":["0.00247320692497939","0.000008129750823137272"]}
KL15
{"SL_VEF_APPLIED_VS_BOL_R1_KL15":["0.01890831252229754","0.9008162336184189"]}
I want to get result like this:-
KLOBS
0.00247320692497939
0.000008129750823137272
KL15
0.01890831252229754
0.9008162336184189
How to call json like this ??
I don't know call multiple value data in Json.
Please help me.
Try this
var arr = {
KLOBS: {
SL_VEF_APPLIED_VS_BOL_R1_KLOBS: [
"0.00247320692497939",
"0.000008129750823137272"
]
},
KL15: {
SL_VEF_APPLIED_VS_BOL_R1_KL15: ["0.01890831252229754", "0.9008162336184189"]
}
};
Object.keys(arr).forEach(function(key, value) {
console.log(key);
Object.keys(arr[key]).forEach(function(val) {
arr[key][val].forEach(function(data) {
console.log(data);
});
});
});
In general you could do the following:
var KLOBS = {"SL_VEF_APPLIED_VS_BOL_R1_KLOBS":
["0.00247320692497939","0.000008129750823137272"]}
values = Object.keys(KLOBS).map(x=>{
return KLOBS[x];
})
values.forEach(x=>x.forEach(y=>console.log(y)))
jsfiddle
Object.keys gives you an array of the keys in an object.
Since I do not know the structure in advance, I assume it better thinking of more than one key.
With the function map, a function is applied to each member of the array returned by Object.keys. This is used to extract the values.
The resulting structure is an array of arrays.
Therefore a double forEach is needed to display every entry of the structure.
I'm trying to iterate over this json encoded array which is a string:
"{"":{"count":{"total":112,"open":0,
"solved":0,
"deleted":106,
"closed":6},
"average_time_open_in_minutes":206,
"tickets_fortnight_week_count":11,
"tickets_last_week_count":15,"trend":1},
"Net2grid":{"count":"total":8,"open":0,"solved":0,"deleted":8},"average_time_open_in_minutes":0,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"Closed_by_merge":{"count":{"total":2,"open":0,"solved":0,"closed":2},"average_time_open_in_minutes":502,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"Analytics":{"count":{"total":1,"open":0,"solved":0,"deleted":1},"average_time_open_in_minutes":26,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"Meter":{"count":{"total":5,"open":5,"solved":0},"average_time_open_in_minutes":0,"tickets_fortnight_week_count":0,"tickets_last_week_count":2,"trend":1},"Installation":{"count":{"total":8,"open":5,"solved":3},"average_time_open_in_minutes":404,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"Other...":{"count":{"total":3,"open":2,"solved":1},"average_time_open_in_minutes":39,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"Meter Offline":{"count":{"total":8,"open":7,"solved":1},"average_time_open_in_minutes":8,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0},"App Usage":{"count":{"total":6,"open":5,"solved":0,"deleted":1},"average_time_open_in_minutes":8,"tickets_fortnight_week_count":0,"tickets_last_week_count":0,"trend":0}}"
An ajax call returns that string and i'm trying to only get the keys like: "app usage" and "Meter Offline" to return like so:
$.get('/ajax/ticket-and-notes-data.php', function (data) {
var problems = getProblems(data);
function getProblems(problems) {
var problemCategories = [];
$.each(JSON.parse(problems), function (key, value) {
if (key != "") {
problemCategories.push = key;
}
});
return problemCategories;
}
});
But I can't get the keys to go into the problemCategories.
I use this to set the categories in a highchart bubble chart and I will use more of the data from the string later.
I need to get this to work first.
The issue is in the way that you're using array.push. You should use array.push(item) instead of array.push = item.
So I have an object array called Banker and i have an array called remove_banker_id,
so in my code
remove_banker_id = [11, 99]. My banker object banker.id has 11 and 99 and i dont want to include them in my third array so how do i do tht?
My current code has this in my javascript file
$.each(data, function( index, banker ) {
$.each(lender_banker_id_array, function(index, banker_id) {
if(parseInt(banker_id) !== banker.id) {
banker_object
.append($('<option>', {value: banker.id, html: banker.name }))
.removeAttr('disabled');
}
})
});
So basically if any lender_banker_id_array is in banker object, do not append it. But with this code it is not working properly. How do i solve this
Try using the jquery utility function $.inArray()
http://api.jquery.com/jquery.inarray/
It returns the index of a value in an array. It returns -1 if the array does not contain the value.
$.each(data, function( index, banker ) {
if($.inArray(banker.id, lender_banker_id_array) == -1) {
banker_object.append($('<option>', {
value: banker.id,
html: banker.name
})).removeAttr('disabled');
}
});
Here I have this code jquery:
JSON.stringify($("#vrsta_rada").select2("data"));
and I get this JSON file:
"[{"id":2,"text":"setva/zasad"},{"id":6,"text":"orezivanje"},{"id":8,"text":"skladistenje"}]"
How I can use .each function to get this string: data[0].text, data[1].text ...
so to get string from JSON above: setva/zasad, orezivanje, skladistenje
?
I'm not sure why you are stringifying the data if you want to manipulate it. So this will work:
var data = $("#vrsta_rada").select2("data");
$.each(data, function(key, value) {
var text = data[key].text
//or
var text = value.text
});
Is this what you are looking for? That will iterate through the list. I prefer to use data[key] because you can then manipulate the data and it will change the original whereas value only makes a copy.
Try
$.each(data, function(index, value) {
alert(value.text)
});
I have an array of objects that should be looking like this...
[{"Name":"blah","Description":"blah"},{"Name":"blah2","Description":"blah2"}]
Using Javascript/jQuery, how can I get the key/value pairs? I've tried many different ways but to no avail. When I try to get the length, it always returns the character count and iterates through each character and not the actual count of objects? When I run this, it returns an alert for [object Object], [object Object] spelled out by each character....
function DisplayItems(data) {
$.each(data, function () {
$.each(this, function (key, value) {
alert(value);
});
});
}
Now that I look at this, is it not the array of objects I'm expecting? How can I actually return the string so I can actually see what's really in it and maybe go from there?
**EDIT:
This is my function to get the orders (cut out the crap and showing you an alert)... I call jQuery.Ajax and pass the returned data to displayOrders(data). The orders have a composite property of Items containing a list of Item.
function displayOrders(data) {
$('#gdvOrders tbody').empty();
for (var key in data.d) {
alert(data.d[key].Items);
}
This is what I passed to displayItems, what you see in the alert function. I display the Orders in one table (hiding some columns including the Items), and want to display the Items for each order in another table when they select a row in the orders table. In the function shown above I can write...
data.d[key].OrderId
and it will display as normal. How do I display the properties for each item?
The jQuery.Ajax function is set to content-type: 'application/json; charset=utf-8' and this is where I get the orders from...
[WebMethod]
public static List<Order> GetOrdersByDept(Department department, Filter filter, DateTime? dateFrom = null, DateTime? dateTo = null)
{
return OrderLists.GetOrdersByDepartment((Department)department, (Filter)filter, dateFrom, dateTo);
}
See this is working:
data=[{"Name":"blah","Description":"blah"},{"Name":"blah2","Description":"blah2"}]
data.forEach(function(i,j){
console.log("Name :"+i.Name+" Description :"+i.Description);
})
Using JavaScript, simply use the for .. in loop
for(var i = 0; i < data.length; i++) {
var obj = data[i];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
alert(key + " = " + obj[key]);
}
}
}
Fiddle here - http://jsfiddle.net/XWsvz/
Now that I look at this, is it not the array of objects I'm expecting? How can I actually return the string so I can actually see what's really in it and maybe go from there?
If the object is being returned as a string, you can simply alert it. However, if your function is being passed an unknown object, you can always convert it back to a JSON string and alert it so that you can visualize the structure:
function displayItems(data) {
alert(JSON.stringify(data));
...
}
As a sidenote, I changed the first letter in your function to a lowercase letter to match naming conventions for functions in JavaScript.
Looks like you're close:
function DisplayItems(data) {
console.log('data is: ', JSON.stringify(data));
$.each(data, function (key, arrayElement, index) {
console.log('arrayElement ' + index + ' is: ', JSON.stringify(arrayElement));
$.each(arrayElement, function (key, value) {
console.log('key: ' + key + ' val: ' + value);
});
});
}
http://jsfiddle.net/ux9D8/
With your data this gives me the following output:
data is: [{"Name":"blah","Description":"blah"},{"Name":"blah2","Description":"blah2"}]
arrayElement undefined is: {"Name":"blah","Description":"blah"}
key: Name val: blah
key: Description val: blah
arrayElement undefined is: {"Name":"blah2","Description":"blah2"}
key: Name val: blah2
key: Description val: blah2