get string of elements by name - javascript

I'm newbee in JS so i need your help.
I have this JSON code :
{
"data": {
"people": [
"get_my_obj": [
{
"yearcome": 2006,
"email": "email1#test.com",
"came": "1.12",
"name": "Alex "
},
{
"yearcome": 2010,
"email": "email2#test.com",
"came": "1.12",
"name": "John"
},
{
"yearcome": 2012,
"email": "email3#test.com",
"came": "1.12",
"name": "Max"
}
]
}
}
How i can get string of elements with emails?
For example var a = email1#test.com;email2#test.com;email3#test.com;
Thats really hard for me because if i have only 2 objects so string must show 2 emails if 3 objects show 3 emails.
Please help me find the way how to do it

var s = { data: ....};
var mails = s.data.people.get_my_obj.map(function (e) { return e.email; }).join(';');
console.log(mails);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Related

How can I pull a key value from JSON API only if another key name within the group matches?

So this is whats on the JSON file:
{
"page": 1,
"total_pages": 10,
"listings": [
{
"name": "Bob",
"occu": "Entry",
"team": "Blue",
"sec": 3,
"days": 16
},
{
"name": "Tom",
"occu": "Advance",
"team": "Main",
"sec": 1,
"days": 23
},
This continues on with hundreds of other entries...
How can I pull the value of "days" only if the "name" is Tom for example.
Sorry if this is a primitive question, im just getting started on developing and im working on a quick project that will help my local sports team and im quite not that advanced with scripting or APIs. Thanks
To pull the value of "days" only if the "name" is Tom need to filter by'Tom' name and then map days
let listings = [
{
"name": "Bob",
"occu": "Entry",
"team": "Blue",
"sec": 3,
"days": 16
},
{
"name": "Tom",
"occu": "Advance",
"team": "Main",
"sec": 1,
"days": 23
},
{
"name": "Tom",
"occu": "Advanddce",
"team": "Maiddn",
"sec": 1,
"days": 55
}
];
const result = listings.filter(listing=> listing.name=="Tom").map(listing => listing.days);
console.log(result);
You can make a function like this which loops through an array (listings) of objects:
getDaysFromListings = (listings, name) => {
for (let listing of jsonObj.listings) {
if (listing.name === name) {
return listing.days;
}
}
// didn't find 'Tom', return empty string
return '';
}
Then, call your function like this:
// assuming your entire json object is stored in a variable called "json"
let days = getDaysFromListings(json.listings, 'Tom');
Since your variable days comes as a string, you can turn this into a number with +days.
Edit: To add the variable days to html, first, let's assume you want to add the text to a div that looks like this:
<div id="target"></div>
You can use javascript to add the days variable here like this:
// assume you already stored the data in variable
document.getElementById('target').innerHTML = days;
Note: You have to load this javascript after you load the html div above.
As there are many ways to achieve this and am certain this may not be the best method, one way is using Array.prototype.find
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
var array1 = [{
"name": "Bob",
"occu": "Entry",
"team": "Blue",
"sec": 3,
"days": 16
}, {
"name": "Tom",
"occu": "Advance",
"team": "Main",
"sec": 1,
"days": 23
}];
var found = array1.find(function(element) {
return element.name === "Bob";
});
console.log(found.days);

How to get specific array from JSON object with Javascript?

I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.
This is a sample JSON I got:-
"education": [
{
"school": {
"id": "162285817180560",
"name": "Jhenaidah** School"
},
"type": "H**hool",
"year": {
"id": "14404**5610606",
"name": "2011"
},
"id": "855**14449421"
},
{
"concentration": [
{
"id": "15158**968",
"name": "Sof**ering"
},
{
"id": "20179020**7859",
"name": "Dig**ty"
}
],
"school": {
"id": "10827**27428",
"name": "Univer**g"
},
"type": "College",
"id": "9885**826013"
},
{
"concentration": [
{
"id": "108196**810",
"name": "Science"
}
],
"school": {
"id": "2772**996993",
"name": "some COLLEGE NAME I WANT TO GET"
},
"type": "College",
"year": {
"id": "1388*****",
"name": "2013"
},
"id": "8811215**16"
}]
Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
If you know where that element is, then you can just select it as already mentioned by calling
var obj = FACEBOOK_ACTION;
obj.education[2].school.name
If you want to select specifically the last element, then use something like this:
obj.education[ obj.education.length - 1 ].scool.name
Try this,
if (myData.hasOwnProperty('merchant_id')) {
// do something here
}
where JSON myData is:
{
amount: "10.00",
email: "someone#example.com",
merchant_id: "123",
mobile_no: "9874563210",
order_id: "123456",
passkey: "1234"
}
This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.

Dojox Json Query expressions

I am using Dojox to get an array of objects back that meet an expression, I recently changed my service and now it returns more complicated JSON property banes and my query has stopped working. I think I can place a wild card [*]operator to the start of my query string.
Old JSON
[
{
"attributes": [{
"Type": 1,
"User": "Dave",
"Location": "England"
}]
},
{
"attributes": [{
"Type": 1,
"User": "Paul",
"Location": "England"
}]
},
{
"attributes": [{
"Type": 2,
"User": "James",
"Location": "England"
}]
}
]
Old query string
var jam = dojox.json.query(“[?attributes.Type='0'”, data);
This used to work fine but since the JSON has changed the query isn't working
New JSON
[
{
"attributes": [{
somevalue."sometingelse".username.Type: 1,
somevalue."sometingelse".username.User: "Dave",
somevalue."sometingelse".username.Location: "England",
}]
},
{
"attributes": [{
somevalue."sometingelse".username.Type: 1,
somevalue."sometingelse".username.User: "Paul",
somevalue."sometingelse".username.Location: "England",
}]
},
{
"attributes": [{
somevalue."sometingelse".username.Type: 2,
somevalue."sometingelse".username.User: "Steve",
somevalue."sometingelse".username.Location: "England",
}]
}
]
I have tried with a few variations of a similar query with no success
var jam = dojox.json.query(“[?attributes.*Type='0'”, data);
var jam = dojox.json.query(“[?attributes][*Type='0']”, data);
Happy to give further details, thank you in advance.
Essentially a dodgy join we replaced it with a view and set the field names. Another fix was to rename all the layers in the MXD and republished the ArcGIS service.
var jam = dojox.json.query(“[?attributes.Type='0'”, data);
Would work fine after that.

Reading JSON data for a particular ID

I have some JSON data which is in the following format:
[
{
"id": 145,
"Name": "John",
"company_name": "A",
"email": "john#gmail.com",
"country": "USA"
},
{
"id": 500,
"Name": "Mike",
"company_name": "B",
"email": "mike#gmail.com",
"country": "London"
},
{
"id": 100,
"Name": "Sally",
"company_name": "C",
"email": "sally#gmail.com",
"country": "USA"
}
]
Now, suppose I ask the user to enter an id, say 100. Then I need to display all the details for this id.
I am supposed to do this as a part of a web application,where I have to invoke an display the fields of a particular id. This would have been easy if I had a hash like implementation and could display all parameters based on the key-id.
Can anybody tell me how this can be done using such kind of data?
Thanks!
You could use something like this:
(Assuming the you have a variable data with your Json Object).
function getid(id) {
var nobj;
data.forEach(function(obj) {
if(obj.id == id)
nobj = obj;
});
return nobj
}
var neededobj = getid(100);
console.log(neededobj.Name + "\n" + neededobj.email + "\netc...");
But to get the Object you have to loop through your complete array,
until it finds the right Object
see this Fiddle
I think you are looking for Associative Array,
the simplex one would be,
var associativeArray = [];
associativeArray["one"] = "First";
associativeArray["two"] = "Second";
associativeArray["three"] = "Third";
alert(associativeArray.one);
And obviusly you can add json object in value place

How to create JSON using Javascript with sets of data?

I know that I can create this JSON:
[
{
"Title": "Something",
"Price": "234",
"Product_Type": "dsf sf"
},
{
"Title": "hskiuea",
"Price": "4234",
"Product_Type": "sdawer"
}
]
*It uses the values obtained from text inputs contained within an element with the class "newpappend" - As shown below
Using the following code which obtains values from my HTML:
var jsonObj = []; //declare array
$(".newpappened").each(function () {
var p_title = $(this).find('#p_title').val();
var p_price = $(this).find('#p_price').val();
var p_ptype = $(this).find('#p_ptype').val();
jsonObj.push({Title: p_title, Price: p_price, Product_Type: p_ptype});
$(this).remove();
});
But, my goal is to end up with the JSON structured like this:
{
"Product_List": {
"Product": [
{
"Title": "asdf",
"Price": "53",
"Product_Type": "Adfsdf"
},
{
"Title": "asgsd",
"Price": "123",
"Product_Type": "Ntohig"
}
]
}
}
Basically, I am struggling with the correct Javascript to use to reach my goal
You are really close. Start with what you have, and then later:
var output = {
"Product_List": {
"Product": jsonObj
}
}
// output is now what you are looking for.

Categories