How do I group a new array? [duplicate] - javascript

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] }];
}
});
});
}
};

Related

Javascript multidimensional array sort by date [duplicate]

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));

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' } ]

Copy array of objects and filter based on a property [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 3 years ago.
I have an array
Persons:[
{
Name: 'xz',
Job:'abc',
Manager:true},
{
Name: 'xy',
Job:'ac',
Manager:false},
{
Name: 'z',
Job:'a',
Manager:true}
]
I want to filter out the objects for which manager is true and set state i.e. Person_new:[] with this data using setState.
Can you help me out with this?
Because you have a boolean key in your array of objects, you can use filter to pick out only the Persons with manager set to true like so!
const managers = Persons.filter(person => person.Manager)
Use the Array filter method to identify the managers and then use the setState method:
const Persons = [
{ Name: 'xz', Job: 'abc', Manager: true },
{ Name: 'xy', Job: 'ac', Manager: false },
{ Name: 'z', Job: 'a', Manager: true }
];
const managers = Persons.filter(person => person.Manager);
this.setState({ persons: managers });

Find top level key in multidimensional object by value of second level key in javascript [duplicate]

This question already has answers here:
Find by key deep in a nested array
(21 answers)
Closed 5 years ago.
I have and multidimensional object that looks like this:
obj = {
'someString': {
name: 'John',
page: 'some url',
number: 4
},
'someString2': {
name: 'Bill',
page: 'some url',
number: 7
}
}
How do I find the first level key (in this case "someString2") where "number" is equal to 7?
The number is always unique and is the only thing I know beforehand.
Here you go. Using Array.find function to look for the appropriate key.
const numberToLookFor = 7;
const data = {
someString: {
name: 'John',
page: 'some url',
number: 4,
},
someString2: {
name: 'Bill',
page: 'some url',
number: 7,
},
};
const myKey = Object.keys(data).find(x => data[x].number === numberToLookFor);
console.log(myKey);

Categories