javascript/jquery indicate something is a variable - javascript

I have a function that injects user's data into html file
the json format is something like this:
{"Fname":"abc","LName":"cdf", "Phone":"123456";}
this is a function to inject data into html file:
function injectData(data){
$.each(data, function(key, value) {
//notice that in the "data.value", I want 'value' to be a variable
//so that when I loop over the array, data.value will become data.Fname,
//and then data.LName and then data.Phone
$("#"+key).html(' ' + data.value +'');
});
How can I force javascript to interpret 'value' as a variable first before calling data.value in order to get the real value from JSON object? This is a little bit confusing. Hope you understand .Thank you

Write something like:
for(key in data){ $("#"+key).html(' ' + data[key] +''); });

Related

How to create dynamic variables in Angularjs, javascript and other scripting language?

I want to create dynamic variables like Vikas0, Vikas1, Vikas2, Vikas3 and so on.
$.each(data, function(key, value){
$scope.servicesDataList[key].list="Vikas"+key;
// will print Vikas0, Vikas1, Vikas2, Vikas3
console.log($scope.servicesDataList[key].list);
});
Now I want to create dynamic variables. Will any one knows how can I achieve this. I hope I am able to elaborate my question.
You can use.
the syntax would be = $scope["vikas"+key] = []
$.each(data, function(key, value){
$scope["Vikas"+key] = value;
console.log($scope["vikas"+key].value);
will print Vikas0, Vikas1,Vikas2, Vikas3
});
You can with using array of object act as a dynamic variable.
Var conctVar="":
$.each(data, function(key, value){
conctVar= conctVar + "{Vikas"+key + ":Vikas"+value+"},";
});
conctVar=conctVar.slice(0,-1);
$scope.FinalObject=JSON.parse(conctVar);// now the string will convert to a array object
Now you can get the array object looks like below
$scope.FinalObject.Vikas1,// value is vikas1
$scope.FinalObject.Vikas2,// value is vikas2
$scope.FinalObject.Vikas3,//value is vikas3
$scope.FinalObject.Vikas4//value is vikas4

Accessing a value in JSON

I am trying to access the country names from this json -
https://restcountries.eu/rest/v1/all
This is my code for loading that json -
<script>
(function() {
var country = "https://restcountries.eu/rest/v1/all";
$.getJSON( country)
.done(function( data ) {
$.each(data, function(key, value) {
$('#mySelect').empty();
$('#myselect').append('<option>' + data.name + '</option>');
});
});
})();
</script>
The problem may be in data.name statement. I couldn't find any solution on my own. Plus, i am new to JSON. Any help or at least any comment to point my faults will be appreciated.
it should be value.name, data is the array you get from the api, value is each item in the array. also, remove empty as it's remove everything in each loop cyle...
see working code here

How to iterate objects in json whit jQuery

I am returning one JSON response from the server:
{'error':'true',fields:[[{'pk':2,'title':'test'}],{'votes':20,'cant':{1:0,2:3}}]}
Console Dev return
Object { error="true", fields=[2]}
I'm trying to get all the data fields[2], but not work, I'm doing something:
$.each(data.fields, function(i,item){
console.log(data.fields[i]);
})
Question: I know that I'm doing wrong, I want to access all the data in the order fields[2], pk and title.
Thanks.
You can fetch fields[2] using following:
$(data.fields).last()[0] // Give {votes: 20, cant: Object}
which you can use to iterate and get all data as:
var other_data = $(data.fields).last()[0]
$.each(other_data, function(key, value){
console.log('key : ' + key + ' value: ' + value);
});
Your code is need some corrections try this,
Demo
$.each(data.fields[1], function(i,item){
console.log(item);
})

Parsing json using jquery undefined

Hi I am new to json/jquery. Please help.
I have a database with list of cities/states. I get it and in the html document and use json_encode to get the json in a javascript object.
var json_obj = jQuery.parseJSON('<?php echo json_encode($query); ?>');
It looks like:
"[
{"city":"Aaronsburg","state_code":"PA"},
...
{"city":"Abbeville","state_code":"AL"}
]"
I am trying to use the following to access each city/state:
$.each(json_obj, function() {
$("<div>" + json_obj['state_code']+"/div>").appendTo('#test'); // I also tried json_obj.state_code
});
What I get in the output is:
undefined
...
undefined
What i need to do is actually print the city/state
Any help will be appreciated.
Thank you
The curreent value is passed by jQuery as:
$.each(json_obj, function(index, value) {
$("<div>" + value.state_code + "/div>").appendTo('#test');
});
Take a look at the specs.

How do I iterate over object literal array with jquery $.each() method?

how do I iterate over object literal array with jquery $.each() method?
In the chrome debugger, it comes back as "undefined, undefined".
Any help would be really appreciated. thanks!
var links = [
{ className : 'hover', url : 'http://www.yahoo.com' },
{ className : 'hover-2', url : 'http://www.foxnews.com' },
];
loopy(links)
function loopy(obj){
$.each(
obj,
function(){
console.log(obj.className + ', ' + obj.url);
}
);
}
Try:
$.each(
obj,
function(i, val){
console.log(val.className + ', ' + val.url);
}
);
The $.each function parameter takes two arguments -- the index of the current item in the array, and the second is the actual current value of the array.
See the API for some more explanation and examples.
You can see this fiddle to see it in action.
I just wanted to offer a slightly modified version of David's response that will be a little more gentle on the DOM when processing larger objects.
Original answer (above):
$.each(
obj,
function(i, val){
console.log(val.className + ', ' + val.url);
}
);
This solution works, but it generates an entirely new instance of the iterator function for every property in the object. There's no need for that, and I think you'll find more stable performance in larger applications by declaring specific methods to handle that sort of thing.
Try this:
// Process a property in your link object
function links_iterationHandler(i, val) {
console.log(val.className + ', ' + val.url);
}
// Traverse the link object and pass each result off to the method above
$.each(obj, links_iterationHandler);
This essentially does the same thing, but doesn't hammer on the DOM as much. It's not a big deal if you only have a few items in your object. But if you're dealing with a lot of data, like a big recordset loaded via Ajax, it will make a difference for sure.
Cheers!
I would try this:
$.each([52, 97], function(index, value) {
alert(index + ': ' + value);
});
The first property is the index.
http://api.jquery.com/jQuery.each/

Categories

Resources