Ajax get function array element [duplicate] - javascript

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
Hello stackoverflow community, I need help with ajax $.get function. When I recieve array from getlist.php and i alert ir like this alert(data_list); everything works correctly. But when I try to alert like this alert(data_list.id) it doesn't work. Here is how my array looks like in console:
[{"id":"2","name":"Something","type":"horizontal","clicks":"0","start_date":"01/20/2016","end_date"
:"02/19/2016","status":"1","target":"http://","image_url":"http://","pre_exp_email":"0"},{"id"
:"1","name":"None","type":"horizontal","clicks":"2","start_date":"01/20/2016","end_date":"05/19/2016"
,"status":"1","target":"http://wps.us.lt","image_url":"http://....../wp-content/uploads/2016
/01/250by250ad.jpg","pre_exp_email":"0","group_id":["1"],"slots":{"1":"1"}}]
And here is myfunction which calls get function.
function get_list() {
jQuery.get("/wp-content/plugins/wp125/functions/getlist.php", { grouptype:jQuery('#grouptype').val() }, function(data_list){
var str;
alert(data_list.name);
});
}

If data is only a object like
var k={"id":123;"name":"abc"}
you can access id using k.id
but when it is object of Array like
var k=[{"id":123,"name":"abc"},{"id":13,"name":"ab"}];
in that case you have iterate over array something like this
for(i=0;i<k.length;i++)
{console.log(k[i].id)}

Related

Calling a nested function with a string (window[]) in JS [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

JSON object to Array Javascript [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 2 years ago.
Im seeing a lot of answers to this in simple arrays but I need the keys and value. I have a json object but need an array. The object looks like this:
{"Food":"Starbucks", "Job":"Electrician"}
I just need a simple array like this:
["Food" => "Starbucks", "Job" => "Electrician"]
Edit: Im not sure how to type it out. Just need to be able to do an array.map and get the keys and values in javascript.
Here is the final code that I am trying:
const details = jsonObject;
{details.map(function(item,idx){
return<DetailCell>
<Label>{idx}</Label>
<Text style={TextStyle}>{item}</Text>
</DetailCell>
})}
Label should be the key and the Text should be the value.
I think you need this :
[{"Food" : "Starbucks"}, {"Job" : "Electrician"}]
To declare a literal array value is something like this in JSON in node.
{JSONVarWithArrayValue : [0,0,0,0]}

How to access object of an object returned by json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
in a little laravel application I'm working on I'm returning data from an ajax request like this:
return response ()->json ( $subject::where('id', $subject->id)->with('division')->get(['id', 'name']));
This returned something quite like an object that have nested objects. This how my results looks when I log it to the console.
I want to get the name and id of the subject details returned, which in this case is History and 8. Also I want to be able to access the division array and properties of the object it has.
I do this to log the name of the subject console.log(data.name) but in returned I get:
undefined
How can I achieve this?
You have an array of objects, which has one property (division) - which contains another array. So you have to access the array indices
console.log(data[0].division[0].name);
Looking at the object structure we see that this is an array of elements, so it should be data[0].name to fetch History text
I'm assuming data is your entire object.
Try data[0].name

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

Javascript Turn string into array [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 7 years ago.
I want to turn this into an array
"["65747add-afd2-45b5-92e0-150bbe40e6d9", "9c247ea5-6b81-4f47-a50c-42367dedd50b", "c1555363-aca9-4e04-8844-e0180397c72e"]"
I am getting it from the page like this:
$('#layout-uuids').text()
Is there away to just get it as an array or do I need to turn the string into an array somehow?
Thanks!
That string looks like JSON. If it is indeed JSON, all you need is JSON.parse():
var someArray = JSON.parse($('#layout-uuids').text());

Categories

Resources