This question already has answers here:
How to sort an object array by date property?
(25 answers)
How to sort an array of objects by date?
(4 answers)
Closed 12 months ago.
I retrieve an array of objects and I need to format it to make it acceptable for Google Charts. The initial data looks like so
[
{
"itemOne":111,
"itemTwo":1,
"itemThree":"2022-02-01T00:00:00.000Z"
},
{
"itemOne":222,
"itemTwo":2,
"itemThree":"2022-01-01T00:00:00.000Z"
},
{
"itemOne":333,
"itemTwo":3,
"itemThree":"2021-12-01T00:00:00.000Z"
},
]
To start the formatting, I do the following
const chartData = data.lastYear.map(
(item) => ([item.itemThree, item.itemTwo, item.itemOne]),
);
Which now leaves me with this.
[
["2022-02-01T00:00:00.000Z", 1, 111],
["2022-01-01T00:00:00.000Z", 2, 222],
["2021-12-01T00:00:00.000Z", 3, 333],
]
I have been trying however to sort the above by date. My current attempt is pretty bad, and obviously doesnt work.
const chartData = data.lastYear.map(
(item) => ([item.itemThree, item.itemTwo, item.itemOne]),
).sort((a, b) => {
return new Date(b.date) - new Date(a.date);
});
So what would be the best way to sort everything by date?
Thanks
let data = [
{
"itemOne":111,
"itemTwo":1,
"itemThree":"2022-02-01T00:00:00.000Z"
},
{
"itemOne":222,
"itemTwo":2,
"itemThree":"2022-01-01T00:00:00.000Z"
},
{
"itemOne":333,
"itemTwo":3,
"itemThree":"2021-12-01T00:00:00.000Z"
},
]
data.sort((a,b)=>{
return new Date(b.itemThree) - new Date(a.itemThree);
})
This sorts your data array according to date.
Hope this is helpful
Related
This question already has answers here:
Array.push return pushed value?
(7 answers)
Closed 4 months ago.
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject(subject) {
this.subjects = this.subjects.push(subject); //what's wrong with this line
}
}
student1.addSubject('Math');
console.log(student1.subjects);
// logs out 1 instead of ['Math'], .push isn't functioning properly
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject: function(subject) {
this.subjects.push(subject);
}
}
student1.addSubject('Math');
console.log(student1.subjects);
Array.push() returns the new length of the array, not the array itself.
Unless you have a reason to capture this value, you don't need to assign it:
addSubject(subject) {
this.subjects.push(subject);
}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 months ago.
I have an application, where I use MongoDB, Express and MongoDB. On server side I received data by using find in such look - array of objects(JSON), which is named upProbations. For example, I find information about Max:
[
{
Name: 'Max',
DateOne: 2019-01-01T00:00:00.000Z,
DateTwo: 2019-04-01T00:00:00.000Z,
Info: 'Cheerful'
}
]
I want to change information a bit - fields DateOne and DateTwo. They have both types Date and I want to leave only 2019-01-01 and same for DateTwo, so i don't need time(hour, minute, second). The result should look like this:
[
{
Name: 'Peter',
DateOne: 2019-01-01,
DateTwo: 2019-04-01,
Info: 'Cheerful'
}
]
This result I will futher use for antoher calculations. I have tried such code, but it's not working:
upProbations = upProbations.map(function(a) {
a.DateOne = a.DateOne.toISOString().replace('T', ' ').substr(0, 10);
return a;
})
console.log(upProbations);
In which way I can do this?
let upProbations = [
{
Name: "Max",
DateOne: 2019-01-01T00:00:00.000Z,
DateTwo: 2019-04-01T00:00:00.000Z,
Info: "Cheerful",
},
];
upProbations = upProbations.map((eachObject) => {
const { DateOne, DateTwo, Name, Info } = eachObject;
return {
Name,
DateOne: DateOne.toString().split("T")[0],
DateTwo: DateTwo.toString().split("T")[0],
Info,
};
});
console.log(upProbations);
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
Array looks list :
[
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
]
I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.
You can use find like:
const data = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
];
console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));
Reference:
Array.prototype.find()
result = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
].find(item => item.Uid === '673bjhbjhdcsy')
This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 1 year ago.
I want to convert JSON array to a single object. PFB the details
Array:
[{ "item-A": "value-1" }, { "item-B": "value-2" }]
Expected Result:
{ "item-A": "value-1", "item-B": "value-2" }
I have tried following options but result is not what I was expecting
let json = { ...array };
json = Object.assign({}, array);
json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});
Result:
{"0":{"item-A":"value-1"},"1":{"item-B":"value-2"}}
You can use Object.assign and spread the array
const arr=[{ "item-A": "value-1" }, { "item-B": "value-2" }];
console.log(Object.assign({},...arr));
You can use reduce like how you did it with more attention like this:
let array = [{ "item-A": "value-1" }, { "item-B": "value-2" }];
let object = array.reduce((prev, curr) => ({ ...prev, ...curr }), {});
console.log(object);
This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Closed 4 years ago.
object is not pushing into array.
groupEventsArray=[]
groupEvents=(eventOnDate)=>{
for(i=0;i<eventOnDate.events.length;i++){
for(j=0;j<eventOnDate.events.length;j++){
if(eventOnDate.events[i].start==eventOnDate.events[j].start)
this.groupEventsArray.push(eventsOnDate.events[i])
}
}
console.log(JSON.stringify(this.groupEventsArray))
}
No error is coming but I think it is stucked in infinite loop.
Any help will be appreciated
Consider using forEach() method. The forEach() method calls a provided function once for each element in an array, in order.
Note: forEach() does not execute the function for array elements without values.
Syntax
array.forEach(function(currentValue, index, arr), thisValue)
Example
var numbers = [4, 9, 16, 25];
function myFunction(item, index) {
console.log(item, index);
}
numbers.forEach(myFunction)
Looping over an array like this help avoid infinte loop.
Sounds like a basic group array by key question. You could do it in the following way:
const data = {
fullDate: '2018-10-26T09:30:00.000Z',
events: [
{
eventId: '43460',
start: '1540525500',
},
{
eventId: '43461',
start: '1540525500',
},
{
eventId: '43462',
start: '1540525500',
},
{
eventId: '43463',
start: '1540525510',
},
],
};
const createKey = (t) => t.start;
console.log(
Object.values(
data.events
.map((o) => [o, createKey(o)])
.reduce((result, [item, key]) => {
result[key] = result[key] || [];
result[key].push(item);
return result;
}, {}),
),
);
But the question is probably a duplicate of this one (look for answers not using lodash)