Javascript multidimensional array sort by date [duplicate] - javascript

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Closed 3 months ago.
I've a parent array which has some objects in it and those objects have a property called created_at now I want to sort the parent array by the date.
let parent_array = [ { id: 1, date: '2022-12-01T20:34:52.000000Z' }, { id: 2, date: '2022-12-06T20:34:52.000000Z', }, ];
Now I want to sort parent_array by date value.
I tried this method
parent_array.filter((node: any) => {
return node.sort((a: any, b: any) => {
return new Date(b.created_at) - new Date(a.created_at);
});
})
But I'm getting an error "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)"
Thank You
Sakib

let parent_array = [ { id: 1, date: '2022-12-01T20:34:52.000000Z' }, { id: 2, date: '2022-12-06T20:34:52.000000Z', }, ];
const tcx = (a: any, b: any) => {
return new Date(b.date).getTime() - new Date(a.date).getTime();
};
console.log(parent_array.sort(tcx));

Related

Insert object into dynamic key nested array [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

I have array with objects, I need to fetch all the objects that matches the value of userId starts with 'US' [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed last year.
Below is the array with objects:
myArray:[
{"name":"Ram", "email":"ram#gmail.com", "userId":"HB000006"},
{"name":"Shyam", "email":"shyam23#gmail.com", "userId":"US000026"},
{"name":"John", "email":"john#gmail.com", "userId":"HB000011"},
{"name":"Bob", "email":"bob32#gmail.com", "userId":"US000106"}
]}
I tried this but I am not getting output:
item= myArray.filter(element => element.includes("US"));
I am new to Angular.
let filteredArray = myArray.filter(function (item){
return item.userId.substring(0,2).includes('US')
})
Console.log(filteredArray)
//Output
[ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' },
{ name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]
As noted by #halfer - You need to filter on the property that you are interested in - in this case - 'userId' - you can do this by simply adding the property into the code you already had tried and it will log out the specified items - or alternatively - you can make a utility function that takes the array, property and target string as arguments and this will allo2w you to search / filter other arrays and by any property and target string .
These two options are shown below and both log out the same results.
const myArray = [
{"name":"Ram", "email":"ram#gmail.com", "userId":"HB000006"},
{"name":"Shyam", "email":"shyam23#gmail.com", "userId":"US000026"},
{"name":"John", "email":"john#gmail.com", "userId":"HB000011"},
{"name":"Bob", "email":"bob32#gmail.com", "userId":"US000106"}
]
// option 1 - direct filtering
const matchingItems = myArray.filter(element => element.userId.includes("US"));
console.log(matchingItems);
// gives - [ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]
//option 2 - create a function that takes arguments and returns the matches
const matches = (arr, prop, str) => {
return arr.filter(element => element[prop].includes(str));
}
console.log(matches(myArray, 'userId', 'US'));
// gives - [ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]

Order an array of objects by whether property exists [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have an array of teachers, where each one have a property user with image property, that can contain a string or null value.
How can i order the array, considering the user.image property?
Here is an example of structure.
const teachers = [
{
id: 1,
user: {
id: 10,
image: "image.jgp"
}
},
{
id: 3,
user: {
id: 30,
image: null
}
},
{
id: 2,
user: {
id: 20,
image: "image.jgp"
}
},
{
id: 4,
user: {
id: 40,
image: null
}
},
]
You can use Array.sort and check whether the image property is null:
const teachers=[{id:1,user:{id:10,image:"image.jgp"}},{id:3,user:{id:30,image:null}},{id:2,user:{id:20,image:"image.jgp"}},{id:4,user:{id:40,image:null}}];
const sorted = teachers.sort((a, b) => b.user.image === null ? -1 : 1)
console.log(sorted)

How do I group a new array? [duplicate]

This question already has answers here:
Most efficient method to groupby on an array of objects
(58 answers)
How can I group an array of objects by key?
(32 answers)
Group array items using object
(19 answers)
javascript | Object grouping
(16 answers)
Closed 1 year ago.
How do I group a new array? I have data that needs to be grouped in this order,
There are 3 companies in total:
Apple
Samsung
Xiaomi
I receive data about employees who work in these companies, and they are displayed as in pictures (the second list), but it is necessary that, as in the first case, I try to do it like this:
let uniqueGroups;
export const Group = [
{
title: "Apple",
index: 1,
},
{
title: "Samsung",
index: 2,
},
{
title: "Xiaomi",
index: 3,
},
];
const data = [
{_person: 'Alex', _type: 'Apple'},
{_person: 'Marty', _type: 'Xiaomi'},
{_person: 'Gloria', _type: 'Apple'},
{_person: 'Melman', _type: 'Samsung'},
{_person: 'Bob', _type: 'Xiaomi'},
]
// reserveList - My data (_type.name - Company name, _person, the person who works for this company)
const handleRenderGroupItems = () => {
if (data) {
return Group.map(({ index, title }) => {
return data.filter(({ id, _type, _person }) => {
if (_type=== title) {
uniqueGroups = [{ id, type: _type, persons: [_person] }];
}
});
});
}
};

Sorting an array object by nested values within objects - Javascript [duplicate]

This question already has answers here:
Sorted a javascript array of objects by an object property
(6 answers)
Closed 5 years ago.
I currently have an array object (not sure if this is the accurate name) that is comprised of nested key value pairs. I want to be able to sort this by the values within the nested objects.
For example:
var ObjArray = [
{ id = 1,
info = {
number = 4,
name = "foo"
}
},
{ id = 4,
info = {
number = 12,
name = "bar"
}
},
{ id = 9,
info = {
number = 2,
name = "fizz"
}
}
];
So ideally I could sort this object based on the 'number' property, and the resulting array object would have sub objects ordered by the number value within the info.
I've found a similar question (Sorting an object of nested objects in javascript (maybe using lodash?)) but doesn't account for another level of nested objects.
The sorting function needed is
ObjArray.sort((a,b) => a.info.number - b.info.number);
This will sort them ascending
For descending :
ObjArray.sort((a,b) => b.info.number - a.info.number);
var ObjArray = [{
id: 1,
info: {
number: 4,
name: "foo"
}
},
{
id: 4,
info: {
number: 12,
name: "bar"
}
},
{
id: 9,
info: {
number: 2,
name: "fizz"
}
}
];
ObjArray.sort((a,b) => a.info.number - b.info.number);
console.log(ObjArray);

Categories