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 + ".");
}
});
})();
Related
I have an array with multiple elements that looks like this:
{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
},
This is the exact format I use to send data into high charts, however, the problem is that the chart breaks if the timestamps inside each environment (DR, SIT) are not in order.
How can I sort the 'data' inside each environment by timestamp?
This JSON is generated in PHP and sent through to JavaScript. So I would like to know how to sort the data inside either PHP or JavaScript.
Thanks.
This is actually rather trivial, once you know about the usort function. It allows you to define your own sorting function, so you can sort based on any factor of whatever the 2 objects it is passed.
Note that from your example json I had to add a set of square brackets around the whole thing to get PHP to parse it with json_decode .
<?php
// note I had to add square brackets to your
// demo json ....
$json='[{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
}]';
$json_obj_arr=json_decode($json,true);
print_r($json_obj_arr);
print("\n\n");
// a function to handle the sorting itself
// usort will pass it an array(timestamp,datavalue) for both
// $a and $b so we compare the [0] element of each
function cmp($a, $b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
// loop through the array, first is indexed
for($i=0;$i<count($json_obj_arr);$i++){
// then each one of those contains an
// associtive array with an element named ['data'] -
// this is an indexed array that you want sorted
// on the [0] element
usort($json_obj_arr[$i]['data'],"cmp");
}
print_r($json_obj_arr);
print("\n\n");
?>
I am using the Synaptic JS library to perform machine learning. My experience is limited to a few days now so pls forgive my ignorance on the topic. Thank you.
I am using the Architect Network with 3 inputs, 6 hidden, 6 hidden, and 1 output.
My input data is normalised and has variance.
I am using a loop to go through my dataset and to calculate for each point in time the network output. The activation output shows however low<>zero variance between each time unit while the input variance is normal. See the input and output below:
[ [ 0.363820590302, 0.3330488358480717, 0.3116651885975185 ], [
0.5172988208539779 ] ] [ [ 0.48585295937399997, 0.3334973502634277, 0.46007835102578876 ], [ 0.5172953785026767 ] ] [ [ 0.517562951083, 0.33397529189524716, 0.498643366077466 ], [
0.5172944759870963 ] ] [ [ 0.498637746879, 0.3344243285138506, 0.47562693669139866 ], [ 0.517294997539339 ] ] [ [ 0.526600573119, 0.33492075191037307, 0.5096347290849914 ], [ 0.5172941999844842 ] ] [ [ 0.531292644737, 0.3354644096344466, 0.5153411264323275 ], [
0.5172940554705523 ] ]
Where the first three are the inputs and the bold ones the corresponding output.
Am I doing something wrong?
The activation function computes f(Theta*x + b). Now the most likely case is that Theta is somehow 0 or close to 0 and that's why you only see the bias.
You should print out the initial weights of the network.
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 @ extra £4.95 per ticket"
],
[
"ticket_desc",
"Later Owl + Chance For VIP Upgrade"
],
[
"ticket_desc",
"VIP Ticket"
],
[
"ticket_desc",
"VIP Ticket + Collector Ticket @ 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)
};
});
I have a multi-level array containing some objects at its deepest level.
[
[
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
]
]
I would like to use ngRepeat to display all the items in "FUND" or "DAILY" but I'm unsure how to access objects this deep without names for each of the arrays above.
Sorry if this is a basic question but I wasn't able to find an answer elsewhere.
You'll want to get the first element of your two outer arrays.
$scope.obj = [
[
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
]
]
<ng-repeat el in obj[0][0]>
<span>totassets: {{el[0].FUND.totalAssets}}</span>
<span>navdate: {{el[0].DAILY.navdate}}</span>
</ng-repeat>
An issue you have with the array is that even when you ignore the outer arrays, you're still left with two individual arrays a la:
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
And:
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
So you will need two ngRepeat blocks to achieve what I assume you want to achieve as well as going one level deeper to actually access the values you want.
Here's a quick plnkr to demonstrate what I mean: http://plnkr.co/edit/ArCh8q8w2JoXsg107XwP?p=preview
I'm creating an index file in JSON, which I'm using as a sort-of-database index for a javascript application I'm working on.
My index will look like this:
{
"_id": "acomplex_indices.json",
"indexAB": {
"title": {
"Shawshank Redemption": [
"0"
],
"Godfather": [
"1"
],
"Godfather 2": [
"2"
],
"Pulp Fiction": [
"3"
],
"The Good, The Bad and The Ugly": [
"4"
],
"12 Angry Men": [
"5"
],
"The Dark Knight": [
"6"
],
"Schindlers List": [
"7"
],
"Lord of the Rings - Return of the King": [
"8"
],
"Fight Club": [
"9"
],
"Star Wars Episode V": [
"10"
],
"Lord Of the Rings - Fellowship of the Ring": [
"11"
],
"One flew over the Cuckoo's Nest": [
"12"
],
"Inception": [
"13"
],
"Godfellas": [
"14"
]
},
"year": {
"1994": [
"0",
"3"
],
"1972": [
"1"
],
"1974": [
"2"
],
"1966": [
"4"
],
"1957": [
"5"
],
"2008": [
"6"
],
"1993": [
"7"
],
"2003": [
"8"
],
"1999": [
"9"
],
"1980": [
"10"
],
"2001": [
"11"
],
"1975": [
"12"
],
"2010": [
"13"
],
"1990": [
"14"
]
}
}
}
So for every keyword (like Pulp Fiction), I'm storing the matching document-id(s).
My problem is with integers/numbers/non-string data, like the release year in the above example. This is stored as a string, while I had hoped it would be stored as a number.
I'm creating the index entries like this:
// indices = the current index file
// doc = the document to update the index with
// priv.indices = all indices defined for this application instance
// priv.indices.fields = index fields e.g. "year", "director", "title"
// priv.indices.name = name of this index
priv.updateIndices = function (indices, doc) {
var i, j, index, value, label, key, l = priv.indices.length;
// loop all indices to add document
for (i = 0; i < l; i += 1) {
index = {};
index.reference = priv.indices[i];
index.reference_size = index.reference.fields.length;
index.current = indices[index.reference.name];
for (j = 0; j < index.reference_size; j += 1) {
label = index.reference.fields[j]; // like "year"
value = doc[label]; // like 1985
// if document has a label field (e.g. doc.year = 1985)
if (value !== undefined) {
// check if the index file already contains an entry for 1985
index.current_size = priv.getObjectSize(index.current[label]);
if (index.current_size > 0) {
// check if the document id is already in the index
// in case the data is updated (e.g. change 1982 to 1985)
key = priv.searchIndexByValue(
index.current[label],
doc._id,
"key"
);
if (!!key) {
delete index.current[label][key];
}
}
// create a new array if 1985 is not in the index yet
if (index.current[label][value] === undefined) {
index.current[label][value] = [];
}
// add the document id to an existing entry
index.current[label][value].push(doc._id);
}
}
}
return indices;
};
This works fine, except that fields I want to store as non-strings (integers, numbers or datetime), like the year in the above example end up as strings in my index.
Question:
Is it at all possible to store "non-string" types in a JSON document? If so, can I also store the key of a key/value pair as a "non-string" element.
If not, would I have to add a parameter to my index definitions declaring the type of each key in order to modify the key-string when I run into it or is there a better way to do it?
Thanks!
Is it at all possible to store "non-string" types in a JSON document?
Yes. The value of a property can be a string, number, boolean, object, array or null (undefined is a notable exception - it's a native JavaScript type but it's not a valid JSON value).
Can I also store the key of a key/value pair as a "non-string" element?
No. The key name must always be a string. However, that doesn't mean you can't parse that string into some other JavaScript type. For example, if you have a string but need a number, you can use the parseInt function, or the unary + operator.
See the JSON grammar for more detail.
no you can't, in JSON keys are strings.
the best you can do is storing string representations of those keys, wether integer or objects(more complicated, you have to build a serialization function).
If you want to use only consecutive integers keys starting from 0, then you can use arrays.
According to the json spec, you can have a number anywhere you could have a value. So the key of an object must be a string, but the value can be a number. Also any of the values in an array can be a number.
The spec is beside the point though; I believe the issue is this line:
index.current[label][value].push(doc._id);
When you read doc._id, that is a string. If you want to store it in the JSON as a number, you need to cast it:
index.current[label][value].push(parseInt(doc._id, 10));
Also note that having just numbers as IDs is not valid HTML.