Extract Lat/Long data from the javascript array - javascript

I want to extract Lat/Long values from the below mentioned array. Please help me.
var products = {"PolygonCords":"[[51.65040675460229,0.034332275390625],[51.613752957501,0.028839111328125],[51.61034179610213,0.1812744140625],[51.642737480428536,0.157928466796875]]"};

Parse the json string using JSON.parse() and iterate over array using forEach
var products = {
"PolygonCords": "[[51.65040675460229,0.034332275390625],[51.613752957501,0.028839111328125],[51.61034179610213,0.1812744140625],[51.642737480428536,0.157928466796875]]"
};
JSON.parse(products.PolygonCords).forEach(function(v) {
console.log(v[0], v[1])
})

Related

How to sort object in JavaScript based on date

Input Data
var data = {
"36905b7cb": "(2019-12-26 T 13H-39M-0S) Co-Testing",
"cad5dd7ea": "(2019-12-05 T 16H-38M-9S) Diagnosis",
"sad4217ea": "(2020-03-05 T 16H-38M-9S) Bio-Testing"
}
Expected Output
var data = {
"sad4217ea": "(2020-03-05 T 16H-38M-9S) Bio-Testing"
"36905b7cb": "(2019-12-26 T 13H-39M-0S) Co-Testing",
"cad5dd7ea": "(2019-12-05 T 16H-38M-9S) Diagnosis"
}
we have to arrange the data in the sorted based on date.Not the KEYS
You can use the array util functions like reduce, sort.

Retrieving Serialized Values

I am using JavaScript serializer to send value to JavaScript:
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
var Result = (from c in dt.AsEnumerable()
select new
{
Latitude = c.Field<Decimal>("Latitude"),
Longitude = c.Field<Decimal>("Longitude")
}).ToList();
hdnControl.Value = oSerializer.Serialize(Result);
This results in this kind of value:
[
{"Latitude":19.2094000000,"Longitude":73.0939000000},
{"Latitude":19.2244070000,"Longitude":73.1545760000},
{"Latitude":32.5838493257,"Longitude":132.3632812500},
{"Latitude":59.3331894266,"Longitude":8.6572265625}
]
How can I retrieve these values in a JavaScript function?
Are there any inbuilt method to access it
or do I need to use the .split() function to get the values?
Any help will be appreciated.
This is javascript json array. You can use forEach to retrieve the value using the keys
a.forEach(function(item){
document.write('<pre> Latitute is '+item.Latitude+'</pre>')
})
A complete list of of array methods is available HERE which can be used as required
JSFIDDLE

Looping through JSON returning values

I've json which looks little complex array of json, I want to parse "1238630400000" and "16.10", like this I need all the values. I'm not getting how we can parse all these values.
This is the code I've tried but no luck:
for (var key in myJSON.Stocks) {
alert(myJSON.Stocks[key].stockPrice);
}
var myJSON = {
"Stocks": {
"stockPrice": [
[1238630400000, 16.10],
[1238716800000, 16.57],
[1238976000000, 16.92],
[1239062400000, 16.43],
[1239148800000, 16.62],
[1239235200000, 17.08],
[1239580800000, 17.17],
[1239667200000, 16.90],
[1239753600000, 16.81],
[1239840000000, 17.35],
[1239926400000, 17.63],
[1241049600000, 17.98]
]
}
}
Can someone help how can i get all these values?
You can get the values by doing a simple forEach on the stockPrice array
myJSON.Stocks.stockPrice.forEach(function(data) { console.log(data[0], data[1]); });
Here is the simplest way:
var csv = myJSON.Stocks.stockPrice.map((o)=>o.join()).join();
console.log(csv);

passing array of objects from javascript to php

So I built an array of objects for passing to PHP, but I am wondering wether this is the cleanest way to pass them and will be the easiest to deal with in PHP.
I am partly thrown off by the fact that the number of group components are variable based on what my google maps reverse-geocoder returns.
Each group will be inserted as a separate row into MySql with the parameters of 'name' and 'type'
var neighborhood = extractLongFromAddress(results[0].address_components, "sublocality");
var town = extractLongFromAddress(results[0].address_components, "locality");
var stateShort = extractShortFromAddress(results[0].address_components, "administrative_area_level_1");
var stateLong = extractLongFromAddress(results[0].address_components, "administrative_area_level_1");
var country = extractLongFromAddress(results[0].address_components, "country");
var groups=[];
if(town && stateShort){
groups.push({name: town+", "+stateShort,
type:"city"
});
}
if(neighborhood && stateLong){
groups.push({name: neighborhood+", "+stateShort,
type:"neighborhood"
});
}
if(stateLong){
groups.push({name:stateLong,
type:"state"
});
}
if(country){
groups.push({name:country,
type:"country"
});
}
console.log(groups);
sincere thanks for any help. It is greatly appreciated.
Just convert the array to a JSON string by JSON.stringify() and send it to php; In PHP you'll do json_decode()

Structuring php array to pass to google pie charts arrayToDataTable

Could someone please tell me how to structure my php array so that i can plug it straight into the Google pie chart API? Here is my current code:
PHP:
// class containing sql
$browser_data = browser_data();
// array to populate titles
$data_array['titles'] = array('title', 'amount');
//array to populate data
foreach($browser_data as $k=>$val) {
$data_array[$k] = array($k, $val['examples']);
}
JS:
data_array = <?=json_encode($data_array)?>;
var data = google.visualization.arrayToDataTable(data_array);
You need to remove 'titles' and '$k' from the array keys, as json_encode will create an object from an associative array instead of an array. This is what you need:
// array to populate titles
$data_array[] = array('title', 'amount');
// array to populate data
foreach($browser_data as $k=>$val) {
$data_array[] = array($k, $val['examples']);
}
If your data source outputs numbers as strings (some databases, including MySQL, do this), you need to add JSON_NUMERIC_CHECK to the json_encode call:
data_array = <?=json_encode($data_array, JSON_NUMERIC_CHECK)?>;

Categories