issues appending jsons to vars - javascript

So i have this json coming back from my server:
[
[
"formatted_sum_fees",
"£6.00"
],
[
"formatted_price",
"£60.00"
],
[
"formatted_sum_fees",
"£8.00"
],
[
"formatted_price",
"£120.00"
],
[
"formatted_price",
"£240.00"
],
[
"formatted_sum_fees",
"£3.20"
],
[
"formatted_sum_fees",
"£2.86"
],
[
"formatted_sum_fees",
"£2.50"
],
[
"ticket_desc",
"Later Owl Ticket"
],
[
"ticket_desc",
"Later Owl Ticket+Collector Ticket &#64 extra £4.95 per ticket"
],
[
"ticket_desc",
"Later Owl + Chance For VIP Upgrade"
],
[
"ticket_desc",
"VIP Ticket"
],
[
"ticket_desc",
"VIP Ticket + Collector Ticket &#64 extra £4.95 per ticket"
],
[
"ticket_desc",
"Skydeck Package"
],
[
"ticket_desc",
"5 Person Skydeck Table"
],
[
"ticket_desc",
"7 Person Skydeck Table"
],
[
"ticket_desc",
"10 Person Skydeck Table"
]
]
Now what im wanting is my ajax call to put these into a table. The first fees and price goes with the first ticket_desc. Each and everytime! The code i have the append to table code. ITs getting the price, fees and description into vars. How do i go about doing this?
This is how i was attempting to do this
success: function(json) {
for (var i = 0; i < json.length; i++) {
var section = json[i][0];
if (section == "ticket_desc") {
var ticket = json[i][1];
debugger;
}
However i ran into issues where the ticket_desc isn't matching up, However i didnt think it would work!
Thanks
Sam

It's not exactly clear to me what the eventual data format is supposed to be, and there are probably many different ways of processing your json, but let's try:
I'd prefer an array of objects that have a description and fee property. For example:
var ticket = {
description: "Later Owl Ticket",
fee: "£6.00"
};
Now, if I understand your data correctly, the order of the array in the json determines the matches between fees and descriptions.
If this is the case, and you can be certain there are no missing values, a straightforward solution could be:
var nrOfTickets = json.length / 2;
var fees = json.slice(0, nrOfTickets); // First half
var descriptions = json.slice(nrOfTickets); // Second half
var tickets = descriptions.map(function(descrArr, index) {
var feeArr = fees[index];
return {
description: descrArr[1],
fee: feeArr[1]
};
});
If you want to make it shorter, you could do:
var tickets = [];
for (var i = 0, l = json.length / 2; i < l; i += 1) {
tickets.push({
fee: json[i][1],
description: json[i+l][1]
});
};
If the fees and descriptions are scrambled, you can sort them like so:
var descriptions = [],
fees = [];
json.forEach(function(arr) {
if (arr[1] === "ticket_desc") {
descriptions.push(arr);
} else {
fees.push(arr)
};
});

Related

How edit polygon from static coordinates Leaflet.js

Hell, I have static array I draw on map and I want to make it editable. could you help me with any idea?
there is may code
var pol = [
[ 46.655639, 24.804925 ],
[ 46.655389, 24.804815 ],
[ 46.655507, 24.804573 ],
[ 46.655752, 24.804689 ],
[46.655639, 24.804925 ]
];
var poly = L.polygon(pol,{color: 'red'});
poly.addTo(map);
var getPolygonCenter = poly.getBounds().getCenter();
getLocation = [getPolygonCenter.lat, getPolygonCenter.lng];
map.setView(getLocation, defaultZoomLevel);

Google sheets API fetch the value of Merged cells

I am using Google Sheets API to display the data from my Google Spreadsheet in my javascript application. Have developed by refering this documentation. I could able to read the values from my spreadsheet but for some of the columns the cells are merged as below.
I am getting the Json response as
[
[
"Name",
"Age"
],
[
"John",
"25"
],
[
"Doe"
],
]
My expected output should be
[
[
"Name",
"Age"
],
[
"John",
"25"
],
[
"Doe",
"25"
],
]
Any help ?
To retrieve the value, you have to test if the cell is part of a merged area.
var value = (cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1,1) : cell).getValue();
Modify the script as following
appendPre('Name, Major:');
var lastData=''
for (i = 0; i < range.values.length; i++) {
var row = range.values[i];
if (row[4]!=''){lastData=row[4]}
// Print columns A and lastData, which correspond to indices 0 and the last known value in column E that contains merged cells.
appendPre(row[0] + ', ' + lastData);
}
so that you will recall the last value if the cell is empty

How to sort out list by datetime in angular2

I'm new to angular2 and I'm trying to sort out my food order list by collection time datestamp coming from the database.
let say the order object is below and in my template I want to sort it with earliest collectiontime in the example below it would be id: 2454
var awaitingPicking = [
{
"id": "2452",
"OrderLineGroups": [
{
"CollectionDetails": {
"CollectionFrom": "2017-03-21T11:00:00.317"
}
}
]
},
{
"id": "2454",
"OrderLineGroups": [
{
"CollectionDetails": {
"CollectionFrom": "2017-03-21T11:00:00.317"
}
}
]
}
]
I tried using this example but it didn't sort out the list by CollectionFrom
http://plnkr.co/edit/DU6pxr?p=preview
Here's my example using the above plnkr
https://plnkr.co/edit/TYXh2iCDseJNnT0DwKA7?p=preview
Expected result should be like --
- 2454 2017-03-21T11:00:00.317 // this has an early collection at (11:00 am)
- 2452 2017-03-21T11:40:00.317

Syphoning out irregularities in a large array using a loop. JS

I have this huge array that is representing opening and closing times. (this stretches for an entire week). I also created a new feature in which it allows users to have holiday hours in which they can change their hours....but also close their doors during a specific day. The problem that I've run across is when a customer chooses to close for a day, the date for the large array becomes a blank array.
Is there an effective way to trigger something if the elements inside one of those arrays return nothing?
[
[],
[
"2015-10-16T13:00:00Z",
"2015-10-16T21:00:00Z"
],
[],
[
"2015-10-18T13:00:00Z",
"2015-10-18T21:00:00Z"
],
[
"2015-10-19T05:00:00Z",
"2015-10-20T05:00:00Z"
],
[
"2015-10-20T13:00:00Z",
"2015-10-20T21:00:00Z"
],
[
"2015-10-21T13:00:00Z",
"2015-10-21T21:00:00Z"
]
]
So up here ^^ is what my large array looks like when a customer chooses to be closed for the day.
You can use .map and check for x.length === 0 condition.
(function() {
var data = [
[],
[
"2015-10-16T13:00:00Z",
"2015-10-16T21:00:00Z"
],
[],
[
"2015-10-18T13:00:00Z",
"2015-10-18T21:00:00Z"
],
[
"2015-10-19T05:00:00Z",
"2015-10-20T05:00:00Z"
],
[
"2015-10-20T13:00:00Z",
"2015-10-20T21:00:00Z"
],
[
"2015-10-21T13:00:00Z",
"2015-10-21T21:00:00Z"
]
];
data.map(function(x, y) {
if (x.length === 0) {
console.log("There is no data in the position: " + y + ".");
}
});
})();

Populate a variable using jQuery based on selections in a drop down

I'm trying to build a nested array in jQuery based on a user's selection from a drop down menu. This will be used in a JSON request at a later date.
So far my code does produce (almost) the required result, however no matter order i select the options from my drop down menu, the output (which i log in the console at the end) is always the same.
$('#comboGenre').change(function () {
var values = $('#comboGenre').val();
var parsedJSON = JSON.parse($data); //Data returned from ajax request
for (var i = 0; i < values.length; i += 1) {
$genreList = parsedJSON.genre[i];
console.log($genreList);
}
});
So if i select RPG and Action from my drop down, the output gives me RPG and Driving. If i selected RPG, Driving and Action (in that order), i get what i would expect RPG, Driving and Action.
So it's just iterating through my JSON, when really it should be returning the 'selected' option.
How can i achieve this?
My JSON looks like this if it's useful:
{"genres": [{
"genre": "RPG",
"publishers": [{
"publisher": "Square",
"games": [{
"game": "FFX",
"rating": [
12, 15
]
}]
}]
},
{
"genre": "Driving",
"publishers": [{
"publisher": "Turn10",
"games": [{
"game": "Forza",
"rating": [
5
]
}]
}]
},
{
"genre": "Action",
"publishers": [{
"publisher": "EA",
"games": [{
"game": "COD",
"rating": [
18, 20
]
}]
}]
}
]}
EDIT:
I've also tried this:
$('#comboGenre').change(function () {
var parsedJSON = JSON.parse($data);
$genreList = "";
$.each(parsedJSON.genres, function(index, value){
$genreList = parsedJSON.genres[index];
console.log($genreList);
});
});
And i end up getting ALL the objects in my JSON, so from here, i'm only wanting to add the selected object to the $genreList variable.
If you broke out some of the logic and created a genre finding function and used the selected string to find the proper object you could then put the object into the variable you will use later. I do some checking to ensure that the genre that has been selected isn't already in my array which is because I am using the multiple select
JSFiddle: http://jsfiddle.net/vkTFq/
Code:
$(function(){
var selectedGenres = [];
var genres =[{"genre":"RPG","publishers":[{"publisher":"Square","games":[{"game":"FFX","rating":[12,15]}]}]},{"genre":"Driving","publishers":[{"publisher":"Turn10","games":[{"game":"Forza","rating":[5]}]}]},{"genre":"Action","publishers":[{"publisher":"EA","games":[{"game":"COD","rating":[18,20]}]}]}]
$('#comboGenre').change(function() {
$(this).find(":selected").each(function() {
var selectedGenre = findGenre($(this).val())
if (!genreAlreadySelected(selectedGenre.genre)) {
selectedGenres.push(selectedGenre);
};
});
console.log (JSON.stringify(selectedGenres));
});
function genreAlreadySelected(genre){
for(var i = 0; i < selectedGenres.length; i++){
if (genre == selectedGenres[i].genre) {
return true;
};
return false;
}
}
function findGenre(genre){
for(var i = 0; i < genres.length; i ++){
console.log(genre)
if(genre == genres[i].genre){
return genres[i];
}
}
};
});

Categories

Resources