Order array of objects by property date [duplicate] - javascript

This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
How to sort an object array by date property?
(25 answers)
Closed 7 months ago.
how can i order this array of objects:
var arr= [
{ name: 'Mario', date: '/Date(16487438090000)/' },
{ name: 'Mario', date: '/Date(16987438090000)/' },
{ name: 'Mario', date: '/Date(16887438090000)/' }
]
According to the 'date' property, how can I order from largest to smallest? example I want you to return me an array like this:
[
{ name: 'Mario', date: '/Date(16987438090000)/' },
{ name: 'Mario', date: '/Date(16887438090000)/' }
{ name: 'Mario', date: '/Date(16487438090000)/' },
]

Related

Get value inside array and array of objects in JS [duplicate]

This question already has answers here:
JavaScript Recursive Search On An Array Of Objects
(4 answers)
JavaScript recursive search in JSON object
(8 answers)
Find an element in an array recursively
(2 answers)
Closed 6 days ago.
I have a similar array in the below format. I need to be able to find the object based on the id and it has to search for all the nested arrays (parent and child). And, the selectedId could be also be from the id of parent array.
Could anyone please advise?
Excerpt from my code
const cars = [{
id: 1,
name: 'toyota',
subs: [{
id: 43,
name: 'supra'
}, {
id: 44,
name: 'prius'
}]
}, {
id: 2,
name: 'Jeep',
subs: [{
id: 30,
name: 'wranger'
}, {
id: 31,
name: 'sahara'
}]
}]
const selectedId = 31;
const result = cars.find((val) => val.subs.id === selectedId)
console.log(result)
My expected output should be
{id: 31, name: 'sahara'}

JavaScript: Best way to check if duplicate value for a particular key exists in an array of objects [duplicate]

This question already has answers here:
How can I check if the array of objects have duplicate property values?
(15 answers)
Closed 2 months ago.
I have an array of objects, for example:
[
{
id: 1243,
name: "Cola",
isLocationAssign: true,
location: 8
},
{
id: 1243,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
}
]
In the above array of objects we have the same value for location. What would be the best way to find out if duplicate values for a particular key exist in an array of objects? I can't think past multiple loops
let array = [
{
id:1234,
name: "Cola",
isLocationAssign: true,
location: 8
},
{
id: 1234,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
},
{
id: 1235,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
},
{
id: 1235,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 7
}
]
let duplicatedIdsItems = array.filter(a => a.id === 1234);
console.log(duplicatedIdsItems)
var valueArr = duplicatedIdsItems.map(function(item){ return item.location });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});```

What is the most effective way to filter this array? [duplicate]

This question already has answers here:
How to get the difference between two arrays of objects in JavaScript
(22 answers)
Closed 1 year ago.
I have two arrays. My goal is to get a third array with users that exist in usersUpdated but do not exist in oldUsers. I know i have to apply multiple filters, but I do not know how.
const oldUsers = [
{ name: 'Fede', id: 1 },
{ name: 'Marce', id: 2 },
];
const usersUpdated = [
{ name: 'Fede', id: 1 },
{ name: 'Marce', id: 2 },
{ name: 'Ale', id: 3 },
{ name: 'Julian', id: 4 },
];
const expectedValue = [
{ name: 'Ale', id: 3 },
{ name: 'Julian', id: 4 }
];
Convert arrays to objects, use names as keys & ids as values, delete keys of old obj from updated obj then convert the updated obj back to array.

How to sort an array by closest with specific value and order by newest? [duplicate]

This question already has answers here:
How to sort an array of objects by multiple fields?
(38 answers)
How to sort an array of objects with multiple field values in JavaScript
(6 answers)
Sort an array of objects by multiple properties
(3 answers)
Closed 1 year ago.
Basically I want sort an array like this:
[
{ date: '5/18/2020', number: 22 },
{ date: '5/19/2021', number: 20 },
{ date: '5/18/2021', number: 18 },
{ date: '5/18/2021', number: 23}
]
I using this to get closest items with 20:
const results = data.sort((a, b) => {
return Math.abs(20- a.number) - Math.abs(20- b.number);
});
The results should like this:
[
{ date: '5/19/2021', number: 20 },
{ date: '5/18/2020', number: 22 },
{ date: '5/18/2021', number: 18 },
{ date: '5/18/2021', number: 23}
]
But I also want get the results by newest items this:
[
{ date: '5/19/2021', number: 20 },
{ date: '5/18/2021', number: 18 },
{ date: '5/18/2020', number: 22 },
{ date: '5/18/2021', number: 23}
]
How do I do that?
Thanks in advance!

How to sort array of objects have date properties? [duplicate]

This question already has answers here:
How to sort an array of objects by date?
(4 answers)
Closed 2 years ago.
I have an array :
const arr = [
{ name: 'abc', date: '30/03/2014' },
{ name: 'cde', date: '30/03/2015' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'xyz', date: '17/09/2014' },
];
How can I sort this array so that the output would be like this:
const arr = [
{ name: 'cde', date: '30/03/2015' },
{ name: 'xyz', date: '17/09/2014' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'abc', date: '30/03/2014' },
];
// sort the array with date in latest first.
Using Array#sort with your own sorting comperator. For this split the dates and build with taht in the right sequence a new Date which can be compared.
const arr = [
{ name: 'abc', date: '30/03/2014' },
{ name: 'cde', date: '30/03/2015' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'xyz', date: '17/09/2014' },
];
arr.sort((a,b) => {
let tempA = a.date.split('/');
let tempB = b.date.split('/');
return ((new Date(tempB[2],tempB[1],tempB[0])) - (new Date(tempA[2],tempA[1],tempA[0])));
});
console.log(arr);

Categories