This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 3 years ago.
I need to sort list of items by its priority(ascending order) ?
var priorityIssuesList = [
{ name: "Danger", status: "RED", priority:4},
{ name: "Moderate", status: "BLUE", priority:2},
{ name: "Safe", status: "GREEN",priority:1},
{name: "Warning", status: "Yellow",priority:3}
]
sorting should be in ascending order or status
[
{"name":"Safe","status":"GREEN","priority":1},
{"name":"Moderate","status":"BLUE","priority":2},
{"name":"Warning","status":"Yellow","priority":3},
{"name":"Danger","status":"RED","priority":4}
]
what is the best way to do this?
try Array.sort() functionality
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for ascending order
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for descending order
console.log(priorityIssuesList);
Related
This question already has answers here:
How can I create every combination possible for the contents of two arrays?
(16 answers)
Cartesian product of multiple arrays in JavaScript
(35 answers)
Closed 1 year ago.
Ive got 3 parent arrays that could sometimes be different lengths which are called Options
Each Option has values array which can also be different lengths
Example of this would be something like this
[
{
name: "Option 1",
values: ["Blue","Red","Orange"]
},
{
name: "Option 2",
values: ["Small","Medium","Large"]
},
{
name: "Option 3",
values: ["Cotton","Satin"]
}
]
The outcome should generate an array like this
[
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Small/Satin",
price: "10.00"
},
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Medium/Satin",
price: "10.00"
},
]
And So on.
Ive tried mapping through the options but knowing the size of the multiple arrays is what I couldnt figure out
SOLUTION
I managed to get a solution
product.options.reduce(
(a, b) => a.flatMap((x) => b.values.map((y) => [...x, y])),
[[]]
);
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
JavaScript - get array element fulfilling a condition
(10 answers)
How to find first element of array matching a boolean condition in JavaScript?
(14 answers)
Closed 2 years ago.
I have array
Game: [
{ game: 'SS', status: 2 },
{ game: 'CD', status: 2 },
{ game: 'AS', status: 2 },
{ game: 'SM', status: 2 },
{ game: 'GTA', status: 1 },
]
how can I find the GTA's status is equal to 1 using find method in js?
Game.find(game => game.status===1)
You can use filter method:
const games = Game.filter(gta => gta.status == 1 );
This question already has answers here:
Sort array by firstname (alphabetically) in JavaScript [duplicate]
(23 answers)
Closed 3 years ago.
I want to filter the column in alphabetical order
here is the code to filter by the size
how to do
thank you
const columns = [{
title: 'First Name',
dataIndex: 'first_name',
sortDirections: ['descend', 'ascend'],
key: 'first_name',
width: '20%',
sorter: (a, b) => a.first_name.length - b.first_name.length,
}]
You can use localeCompare()
sorter: (a, b) => a.first_name.localeCompare(b.first_name);
This question already has answers here:
javascript | Object grouping
(16 answers)
Closed 4 years ago.
I have an array of objects like this..
[
{
_id: 123
},
{
_id: 123
},
{
_id: 321
}
]
I want to group similar items by _id and also count how many items of each unique _id there are...
[
{
_id: 123
qty: 2
},
{
_id: 321
qty: 1
}
]
Whats the best way to do this using javascript?
You can do this in O(n) time using Array.prototype.reduce() and a Map as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
This question already has answers here:
JavaScript merging objects by id [duplicate]
(18 answers)
Closed 6 years ago.
I have an object called probe. It has a property called sensors which is an array.
var probe = {
sensors = [
{ id: 1, checked: false },
{ id: 2, checked: true },
{ id: 3, checked: false },
... //more sensors
],
... //other properties
}
I have separate array which has an updated list of sensors like below.
var updatedSensors = [
{ id: 1, checked: true },
{ id: 3, checked: true }
];
I want to update the sensors array in the probe object from the values in the updatedSensors. How would I do that?
This can be easily achieved by using a couple of for-loops. But for-loops are not the pretty way of iterating in JavaScript, so I was wondering how to do this the preferred way.
Edit:
The objects in the updatedSensors is a subset of objects in probe.sensors. In other words, updatedSensors does not have all the objects (ids) that are there in the probe.sensors, but probe.sensors has all the objects (ids) that are in the updatedSensors.
Thanks.
Try this bit of code:
probe.sensors.map(function (sensor) {
// Loop through updated sensors & match sensor.id so we can reassign val
updatedSensors.map(function (f) {
if (f.id == sensor.id) {
sensor.checked = f.checked
}
})
return sensor;
})