I'm trying to plot D3 timeseries by referring :
http://mcaule.github.io/d3-timeseries/
which takes Date in this format : 'new Date('2013-01-01')
Now, I have following array:
var data =[{
"time": "2017-06-23T23:37:20-07:00",
"value": 25.189767114257663
},
{
"time": "2017-06-23T23:37:30-07:00",
"value": 24.637318860009692
},
{
"time": "2017-06-23T23:37:40-07:00",
"value": 24.896462306379043
},
{
"time": "2017-06-23T23:37:50-07:00",
"value": 24.348000192323468
}]
I need to give this array as input to the d3 timeseries function. But I'm unable to plot anything as above time is not a Date object. How do I append or convert that time string to date object. Because I absolutely need to have that new Date thing otherwise that function is not at all executing.
I tried to convert time into date object using:
data.map(function(ele) {
ele.time = new Date(ele.time);
});
So, this thing appended Zone to all the times. When I tried to give this array as input to d3 function, it doesn't work.
You can map the timestamp to a date object by iterating over data and assigning a new field called date.
data.forEach(function(elem, index, arr) {
arr[index]['date'] = new Date(
arr[index]['timestamp'].split('T')[0]);
});
Pretty basic string splitting, and there are probably more elegant solutions, but here's a start.
Related
I'm having an issue getting my data from my "react-hook-form" web form in the correct format for my api
I'm already cheating by entering in the [] date field of the form which isn't ideal but just trying to get passed this step for now.
I need to send it over like the below DesiredData, basically with array brackets around the entire thing and then the apostrophes '["1-2-2020", "1-3,2020"]' for the dates
DesiredData:[
{
name:"bob"
age:"20"
dates: ["1-2-2020", "1-3,2020"]
}]
CurrentData: {
name:"bob"
age:"20"
dates: '["1-2-2020", "1-3,2020"]'
}
var currentData = {
name:"bob",
age:"20",
dates: '["1-2-2020", "1-3,2020"]'
}
currentData.dates = JSON.parse(currentData.dates)
var desiredData = [currentData];
console.log(desiredData)
In MongoDB collection I have 3 objects. I need to update one variable (date type) in each object. The main task is to increment the date of the objects. For example: all objects have the same variable:
"Time1" : ISODate("2016-01-12T21:37:46.738Z")
My problem is to update the first object with the current date, manually I do it in this way:
$db.getCollection('my.data')({'_id':ObjectId("52e637fca92cf1ec6a73c1e8")}, {$currentDate: {Time1: true}})
The next is to increase the date of the second object by 1 day, I mean to update it with tomorrow date. I couldn't do it through the shell, because $inc doesn't work with Date type.
So, I am lost with javascript
I found how to get it with java script but I don't know how to collect all if this in one script.
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
Thank you for your help.
You could use the $set operator for the other fields, together the the $currentDate operator all within the update object:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
var dayAfterTomorrow = new Date();
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate()+2);
db.getCollection("my.data").update(
{ "_id": ObjectId("52e637fca92cf1ec6a73c1e8") },
{
"$currentDate": { "Time1": true },
"$set": {
"Time2": tomorrow,
"Time3": dayAfterTomorrow
}
}
)
This is a little hard to explain. I got this from server:
{"enable": "15,16,17,18,19,20,21,22,23",
"disable": "{ from: new Date(2014,9,16,23,0), to: new Date(2014,9,16,23,0) }"}
and this is how I try to set the enabled and disables hours to my pickatime object from that JSON object. (http://amsul.ca/pickadate.js/api.htm#method-set-disable-enable)
$.get('/ajax/res/', function(data) {
var picker = $("#rz-time-1").pickatime('picker');
picker.set('enable', [
data.enable
]);
picker.set('disable', [
data.disable
]);
}, 'json');
this doesn't work, however if I put $.parseJSON(data.enable) it works only if there is not commas in my string... otherwise I get unexpected non-whitespace character because of the commas...
The new Date(2014,9,16,23,0) in your JSON is invalid. Also, why is the disable value a string? It looks like it should not.
Instead of using new Date(...), you could possibly use an array:
{
"enable": "15,16,17,18,19,20,21,22,23",
"disable": {
"from": [2014,9,16,23,0],
"to": [2014,9,16,23,0]
}
}
Now you can access the specific parts of what was the illegal new Date.
Given the following json result, how can I convert the validationErrors key/value property into a nice array object in javascript so i can then do things like
errors[0].Key
or
errors[0].Value, etc...
NOTE: If it's easier to do the conversion with jQuery, then I'm happy to use that. Also, I'm getting the data via jQuery -> $.post...)
update:
Here's the actual json data so someone can answer this with JSFiddle please.
{
"aaaa": 0,
"bbbb": 0,
"cccc": null,
"validationErrors": {
"a1_7127763-1c7ac823-61d5-483f-a9ca-4947e9eb8145": "Invalid PropertyType. Please choose any property except Unknown.",
"a2_7127763-1c7ac823-61d5-483f-a9ca-4947e9eb8145": "A State is required. Eg. Victoria or New South Wales.",
"b1_5433417-18b5568a-d18e-45e2-9c63-30796995e2d3": "Invalid PropertyType. Please choose any property except Unknown.",
"b2_5433417-18b5568a-d18e-45e2-9c63-30796995e2d3": "A State is required. Eg. Victoria or New South Wales.",
"c1_6655305-297c57f9-a460-4101-be7d-70c6b9a565d5": "Invalid PropertyType. Please choose any property except Unknown.",
"c2_6655305-297c57f9-a460-4101-be7d-70c6b9a565d5": "A State is required. Eg. Victoria or New South Wales."
}
}
I would take all the keys of the object and then map them to an array.
var arrayOfErrors = Object.keys(objectOfErrors).map(function(errorKey) {
return objectOfErrors[errorKey];
});
You can use map to convert the object to an array:
var errors = jQuery.map(data.validationErrors, function (value, key) {
return {
"Key": key,
"Value": value
};
});
JSFiddle showing this approach: http://jsfiddle.net/WgaFb/1/
If you do not wish to use jQuery, here is a pure JavaScript method:
var errors = [];
for(var key in data.validationErrors) {
errors.push({
"Key": key,
"Value": data.validationErrors[key]
});
}
JSFiddle for this second approach: http://jsfiddle.net/4WXEF/1/
I've parsed a legitimate JSON object and I'm walking through the multidimensional array to first find the year based on a function argument and then the month within that year.
My for loops are nested:
for (year in yearsData) {
//If we've found the correct year...
if (yearsData[year]['#attributes'].name == yearToGet) {
//...walk through that year's child months
for (month in yearsData[year]) {
//This works!
console.log(yearsData[year][month]);
//This also works!
console.log(yearsData[year][month]['#attributes']);
//This does not work! ..but this is what I need!
console.log(yearsData[year][month]['#attributes'].name);
}
}
I can track the object in Firebug's console (with valid output) all the way up to
console.log(yearsData[year][month]['#attributes']);
But the instant I add the type at the end (.name):
yearsData[year][month]['#attributes'] is undefined.
console.log(yearsData[year][month]); returns the valid object, and I can see the valid #attributes listed.
I've checked the spelling multiple times; I've changed the name of the attribute to other things in case it was somehow conflicting with year's attributes..but I got nothin'.
What simple thing am I missing?
The JSON:
{
"year": [
{
"#attributes": {
"name": "2013"
},
"month": {
"#attributes": {
"name": "January"
},
"winner": "None!",
"theme": "Nobody!"
}
}
]
}
Think about using jQuery.grep or jQuery.each to work efficiently on object iteration and analysis. It can help a lot.
http://api.jquery.com/jQuery.grep/
After some more brainbashing, it turned out to be a super derp. I was using month the variable rather than the string 'month' that would ID it.
I'd need to use: console.log(yearsData[year]['month']['#attributes'].name);
To get it. Whoo.