I have an object called added that looks like this:
{
title: "test1",
startDate: "Mon Apr 15 2019 10:30:00 GMT-0500 (Central Daylight Time)",
endDate: "Mon Apr 15 2019 11:00:00 GMT-0500 (Central Daylight Time)",
allDay: false
}
I was trying edit the startDate and endDate field of this object by doing:
added = {
...added,
{added.startDate: "111", added.endDate: "222"}
}
But this gives me an error that says
unexpected token, expected ,
What is the right way of doing this?
When reassigning added as a new object literal, everything inside the {}s needs to be key-value pairs, or it needs to spread (with ...) an object with key-value pairs into the new object. You can't put a plain object into an object literal (unless you spread it), because an object is a value, not a key-value pair.
Change to:
added = {
...added,
startDate: "111",
endDate: "222"
}
You could also do
added = {
...added,
...{
startDate: "111",
endDate: "222"
}
}
which would be valid syntax (but silly do to - easier to just list the new properties in the outer object literal).
Related
How to convert the 1 object with multiple item inside to an array of object? please see the picture below to understand what i meant, thanks
var author = (`SELECT author, title, remarks, status, date FROM Transaction`, 1000, data=>{
let obj = {[author: [], book: [], condition: [], status: [], date: []]}
for(let x = 0; x < data.length; x++){
obj.author.push(data[x][0]);
obj.book.push(data[x][1]);
obj.condition.push(data[x][2]);
obj.status.push(data[x][3]);
obj.date.push(data[x][4]);
}
console.log("obj: ", obj)
return resolve(obj);
})
The Current result of console.log("obj: ", obj)
{
"authors": "testuser,testname",
"books": "440936785,440936694",
"conditions": "Very Good,New,",
"status": "Not Available,Available",
"datepublished": "Mon Mar 28 2022 18:42:24 GMT+0800 (Philippine Standard Time),Mon Mar 28 2022 18:42:39 GMT+0800 (Philippine Standard Time)"
}
What I want result:
{
"authors": "testname",
"books": "440936694",
"conditions": "New",
"status": "Available",
"datepublished": "Mon Mar 28 2022 18:42:24 GMT+0800 (Philippine Standard Time)"
},
{
"authors": "testname",
"books": "440936694",
"conditions": "New,",
"status": "Available",
"datepublished": "Mon Mar 28 2022 18:42:39 GMT+0800 (Philippine Standard Time)"
}
You have a list of rows and want to create a list of objects. That means you have to convert every row to an object. Such a transformation is typically done with Array#map, which applies a function to every element in an array a produces a new array from the return value of that function:
const objects = data.map(row => ({
author: row[0],
book: row[1],
condition: row[2],
status: row[3],
date: row[4],
}));
The library you are using to query the database might also be able to already create an object per row (using the column names) so you don't have to do the mapping yourself.
This is my Array
$scope.tooltipsArray = [
{
date: 2018-10-10T07:03:43.835Z,
text: 'name1'
},
{
date: 2018-09-29T18:30:00.000Z,
text: 'name2'
}
];
How can I update date to locale date format like this.
$scope.tooltipsArray = [
{
date: Wed Oct 10 2018 14:05:27 GMT+0530 (India Standard Time),
text: 'name1'
},
{
date: Sun Sep 30 2018 00:00:00 GMT+0530 (India Standard Time),
text: 'name2'
}
];
I have used map() to do that. But it does not work
var vector = $scope.tooltipsArray.map(function (el) { return new Date(el.date).toLocaleDateString(); });
Can anyone tell me how to do this from map() in JavaScript?
You can use the below code -
$scope.tooltipsArray = [
{
date: "2018-10-10T07:03:43.835Z",
text: 'name1'
},
{
date: "2018-09-29T18:30:00.000Z",
text: 'name2'
}
];
var vector = $scope.tooltipsArray.map(function(el) {return { 'date':new Date(el.date).toString(),'text':el.text}});
console.log(vector);
The output will be like below -
[
{date: "Wed Oct 10 2018 12:33:43 GMT+0530 (India Standard Time)", text: "name1"}
{date: "Sun Sep 30 2018 00:00:00 GMT+0530 (India Standard Time)", text: "name2"}
]
Why is there a .value key after tooltipsArray?
You assigned the array to tooltipsArray, so unless there's a Proxy involved, expect to access the array through $scope.tooltipsArray.
To fix it, just remove .value.
var vector = $scope.tooltipsArray.map(function (el) { return new Date(el.date).toLocaleDateString(); });
1- Remove .value why is there in the first place?
2- You need to change the date inside the object and then return el instead of date if you just want the date to be changed, likewise:
var vector = $scope.tooltipsArray.map(function(el) {
el.date = new Date(el.date).toLocaleDateString();
return el;
});
What map function does is going through array element one by one and run the callback function, so what you have to do is update the whole object or update one entry
el.date = new Date(el.date).toLocaleDateString();
This question already has answers here:
How to sort an array of objects by multiple fields?
(38 answers)
JavaScript sort array by multiple (number) fields
(3 answers)
Closed 5 years ago.
I have an array of JavaScript objects:
var objs = [
{ key: 10, date: Thu Nov 09 2017 22:30:08 GMT+0530 },
{ key: 10, date: Thu Oct 10 2017 22:30:08 GMT+0530 },
{ key: 20, date: Thu Dec 09 2017 22:30:08 GMT+0530 }
];
AND Trying to get the results like this
var objs = [
{ key: 20, date: Thu Dec 09 2017 22:30:08 GMT+0530 },
{ key: 10, date: Thu Oct 10 2017 22:30:08 GMT+0530 },
{ key: 10, date: Thu Nov 09 2017 22:30:08 GMT+0530 }
];
Array should sort based on both key and date, Key should sort based on descending order and date should sort based on ascending order if and only if key is same.
How I can achieve this?
Here date is the Date object, so need to consider date in millisecond not as string
To sort numbers in descending order, you would use a comparison function such as:
function (a, b) { return b - a; }
If you want a backup comparison, use || so that if the first comparison yields 0, you can use the backup comparison. To compare dates in ascending order, use a - b.
objs.sort(function (a, b) {
return b.key - a.key || a.date - b.date;
});
I have JavaScript function called updateLatestDate that receive as parameter array of objects.
One of the properties of the object in array is the MeasureDate property of date type.
The function updateLatestDate returns the latest date existing in array.
Here is the function:
function updateLatestDate(sensorsData) {
return new Date(Math.max.apply(null, sensorsData.map(function (e) {
return new Date(e.MeasureDate);
})));
}
And here is the example of parameter that function receive:
[{
"Address": 54,
"AlertType": 1,
"Area": "North",
"MeasureDate": "2009-11-27T18:10:00",
"MeasureValue": -1
},
{
"Address": 26,
"AlertType": 1,
"Area": "West",
"MeasureDate": "2010-15-27T15:15:00",
"MeasureValue": -1
},
{
"Address": 25,
"AlertType": 1,
"Area": "North",
"MeasureDate": "2012-10-27T18:10:00",
"MeasureValue": -1
}]
The function updateLatestDate will return MeasureDate value of last object in the array.
And it will look like that:
var latestDate = Sat Oct 27 2012 21:10:00 GMT+0300 (Jerusalem Daylight Time)
As you can see the time of the returned result is different from the time of the input object.The time changed according to GMT.
But I don't want the time to be changed according to GMT.
The desired result is:
var latestDate = Sat Oct 27 2012 18:10:00
Any idea how can I ignore time zone when date returned from updateLatestDate function?
As Frz Khan pointed, you can use the .toISOString() function when returning the date from your function, but if you're seeking the UTC format, use the .toUTCString(), it would output something like Mon, 18 Apr 2016 18:09:32 GMT
function updateLatestDate(sensorsData) {
return new Date(Math.max.apply(null, sensorsData.map(function (e) {
return new Date(e.MeasureDate).toUTCString();
})));
}
The Date.toISOString() function is what you need
try this:
var d = new Date("2012-10-27T18:10:00");
d.toISOString();
result:
"2012-10-27T18:10:00.000Z"
If you use moment it will be
moment('Sat Oct 27 2012 21:10:00 GMT+0300', 'ddd MMM DD DDDD HH:mm:SS [GMT]ZZ').format('ddd MMM DD YYYY HH:mm:SS')
I got this document:
{
_id: "ZApHZeqw98uhwqaey",
borrowerId: "DmGQyqenbNt4eBMia",
isSeenByOther: 1,
lenderId: "JsJyvseqiiazGxRuq",
messages: [{
date: Sun Oct 25 2015 19:40:25 GMT+0100 (CET),
from: "JsJyvseqiiazGxRuq",
text: "Hi there"
},{
date: Sun Oct 25 2015 19:40:35 GMT+0100 (CET),
from: "DmGQyqenbNt4eBMia",
text: "Hey!"
}]
}
What I'm trying to do is to just get a boolean value stating whether or not the value of the field from of the last object item in the array: messages is the current user.
I have tried a lot of different mongodb projections such as $slice and $position which essentially inserts in the beginning of the array but not ideal for my case.
You can use underscore's _.last() function as follows:
var doc = MyCollection.findOne({ _id: "ZApHZeqw98uhwqaey" });
var lastElement = _.last(doc.messages);
if ( lastElement.from === Meteor.userId() ){
... do your thing
}