Sorting objects inside an array [duplicate] - javascript

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 8 years ago.
I have an array of objects, and i am basically breaking my head on sorting it,my array is something like dis :
var myArray = [{1:3},{3:19},{5:53},{6:26},{e:53},{c:107},{B: 2},{f: 5}];
But i have to sort this in such a way that my final output is something like this:
myArray = [{c:107},{5:53},{e:53},{6:26},{3:19},{f: 5},{1:3},{B: 2}];
i.e.; based on the value in each object of array element, the array should sorted in descending order.
Thank you in advance.

You can simply use a comparator function in Array.prototype.sort like this
console.log(myArray.sort(function(first, second) {
return second[Object.keys(second)[0]] - first[Object.keys(first)[0]];
}));
Since the key will be different in every object, we get the actual list of keys and taking only the first item in it.

Related

Array accending and descending javascript [duplicate]

This question already has answers here:
Using Javascript to sort an array of numeric arrays
(5 answers)
Closed 9 months ago.
Create a program that will allow you to enter a number of integers to input. Then input values for those integers and store them into an array(Array A). After that, create another array(Array B) that will store the doubled value of elements from the first array. Display the two arrays. Display also the ascending order of the first array and the descending order of the second array.
look into array mapping (and arrow functions to make life easier)
e.g. var newNumbers = numbers.map(n => n*2)
Will return another array with all the elements of the numbers array doubled.
also look into sort() and reverse()
e.g. newNumbers.sort(function(a, b){return a-b}).reverse();
Will sort the array and reverse its order.
You need this comparison thing in the sort() because of they way numbers are processed with sort()

convert array(2) that has sorted object to object [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Closed 1 year ago.
I'm trying to sort an object from db
MyOjbect : {
key: {
val1:"value",
val2:"",
val3:time,
val4:...
...}
}
and I sorted the object by val3
const sorted = Object.entries(MyObject)
.sort((x,y) => {x[1].val3 - y[1].val3} );
and got an array type.
0:(2) ["key" , {val1:...val2:...}]
1:(2) ["key" , {val1:...val2:...}]
...
I tried reduce() to make it back to the object type, but it changes to be in unsorted order
How can I keep the order sorted and change the type to the object?
You can't. An object doesn't keep the sort order as an Array does it. If you want to have the keys sorted you need to use an Array.

Convert an array of objects to an array of values from the object [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have an array of country details:
var countryArr = [{"name":"Afghanistan","alpha2Code":"AF"},{"name":"Åland Islands","alpha2Code":"AX"},{"name":"Albania","alpha2Code":"AL"},{"name":"Algeria","alpha2Code":"DZ"},{"name":"American Samoa","alpha2Code":"AS"},{"name":"Andorra","alpha2Code":"AD"},{"name":"Angola","alpha2Code":"AO"}]
How can I get each value of the name attribute from this array?
So I end up with an array of country names:
["Afghanistan", "Åland Islands"...]
This is a job for Array.map!
const justTheNames = countryArr.map(country => country.name);
Explanation: The Array.map method will iterate through the elements of the array, applying the given callback function to each and returning a new array with the return values from the callback. In this case, it'll iterate over the array of country objects and create a new array with just the country names.

Javascript: how to join two arrays [duplicate]

This question already has answers here:
Javascript Array Concat not working. Why?
(7 answers)
Closed 6 years ago.
I know this has been asked a lot of times, but I cannot get it to work.
I have an empty array a
var a = [];
and an array with an object b
var b = [{
title: 'test'
}]
I want to join them so a will look exactly like b.
The idea is to do this inside a for loop so a would be added a new item each time.
By using a.concat(b), a results in an empty array.
Not sure what I am missing.
Per Array.prototype.concat()
This method does not change the existing arrays, but instead returns a new array.
You need to assign this operation back to a
a = a.concat(b)
You need to assign result of that call to a. a = a.concat(b)

Javascript object traversal in wrong way [duplicate]

This question already has answers here:
How to keep an Javascript object/array ordered while also maintaining key lookups?
(2 answers)
Closed 7 years ago.
I have a json object in occupancy_list as follows :
I am traversing the object as follows :
for(var prop in occupancy_list)
{
console.log(prop);
}
I am getting values in reverse order. Like at first Room 2 then Room 1. How can I fix it ?
Object properties are unsorted and could always be random. If you need order - get the keys, and iterate them that way:
Object.keys(occupancy_list).sort(function(a, b) {
//sort logic
}).forEach(key) {
//logic on each key
});

Categories