What is the best way to showcase the organized values according to their attributes?

How can I sort and display values based on their properties? For example, I want to only show the likes and convert them back into an object so I can use them as properties.

I apologize for the previous edit, this is the updated version with a working simulation.

Any suggestions would be greatly appreciated.

var CatObj=[];
        var CatimgLikes=[1,4,6,10];
        var CatPostBy=["John","Anne","Simon","Sifu"];
        var CatURLArray=["url1","url2","url3","url4"];
        
            //console.log(CatObj[i]);
            //console.log(Cat);
        

        for(var i=0;i<CatimgLikes.length;i++)
        {

            CatObj[i] = {Likes: CatimgLikes[i], postedByName: CatPostBy[i],imgURL: CatURLArray[i]};
            //console.log(instaObj[i]);
            //console.log(instaObj);
        
            //console.log(instaObj.Likes);
        }


        var byLikes = CatObj.slice(0);
        byLikes.sort(function(a,b) 
        {
            return a.Likes - b.Likes;
        });
        byLikes.sort();
        console.log(byLikes);

Answer №1

Is this a good example that addresses your question?

var byLikes = [
  { name: 'herman', Like: 'milk' },
  { name: 'tabitha', Like: 'catnip' }
];

byLikes.sort(sortByLike);

byLikes.forEach(function (cat) {
  console.log(cat.Like);
}
 
function sortByLike(a, b) {
  var result = 0;
  if(a.Like > b.Like) { result = 1; }
  if(b.Like > a.Like) { result = -1; }
  return result;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to handle multiple promises when loading a state in Angular?

When loading the /home state, I need to retrieve all users from the database in order to customize the home controller and view based on the logged-in user. Currently, in the :resolve section of the state configuration, I am fetching all 'posts' ...

How can I update my Node.js version on Ubuntu?

Having trouble installing node version 4.7 on Ubuntu. Everytime I try to upgrade, it shows that node 4.2.6 is already the newest version. I really need to install npm, but it requires node 4.7 or higher. ...

Tips for retrieving data from a concealed input within a div that is being looped through in Angular.js

Currently, I have a controller that sends data to the UI and uses the ng-repeat directive to map them. My next goal is to bind this data with a hidden input form and then send it to another function in the controller when a click event occurs. Any tips on ...

React's memo and/or useCallback functions are not functioning as anticipated

Within my Home Component, there is a state called records, which I utilize to execute a records.map() and display individual RecordItem components within a table. function Home() { const [records, setRecords] = useState<Array<RecordType>>(l ...

Vue-ctl project creation halted by operating system rejection

While working on a Vue-CLI project, I encountered an issue. Has anyone else faced this problem before? Operating system: Windows 10 - Command Prompt (admin rights) @vue-cli version: 4.5.4 npm version: 6.14.6 Here is the command I ran: vue create font_ ...

The Chevron icon is not pointing downwards even though it has already gone upwards

I want to toggle a chevron icon, but nothing seems to be happening. $("span:last").removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up"); When I add this code below the slideToggle function without an if-else section, the icon changes to ...

Issue with Node.js: Unable to access route when utilizing add.post

I am relatively new to Node.js and I am currently working on a multi-page application using Mongo (mlab), Express, and Node.js. My goal is to navigate to a page called addUser that handles POST requests. However, every time I try to access this page, I enc ...

The leave animation for Angular's ngAnimate and ng-view feature appears to be malfunctioning

angular version: 1.6.1 I am attempting to create a fade in/out effect for my ng-view element, however, I am encountering an issue where only the enter animation is functioning properly. This is the relevant HTML code: <main class="main" ng-view>&l ...

How can I place an Object in front of an Array in JavaScript?

Currently, I am working on an Angular project where I need to modify a JSON array in order to display it as a tree structure. To achieve this, the objects in the array must be nested within another object. Desired format / output: this.nodes = [ { id ...

Struggling to verify credentials on CouchDB

I'm currently facing an issue with authentication while trying to access a couch server from a web application served on port 80. The credentials seem to be removed when passing them in the URL and the session cookie isn't sent to the server. He ...

Assistance needed with implementing jQuery tabs

I'm looking to add a link that takes me directly to content within a non-default tab on another page. Here's the code snippet that explains what I need: My Code: This is from my profile_edit.php page: The javascript: <script src="Javascrip ...

The deployment of my Next.js app on Heroku was refused due to an incompatible Node version

Encountering issues while attempting to deploy my app on Heroku, I am faced with the following shortened errors: remote: -----> Creating runtime environment remote: remote: NPM_CONFIG_LOGLEVEL=error remote: NODE_VERBOSE=false remot ...

What is the process for verifying a particular user in AngularJS?

I'm new to AngularJS and I'm a bit confused about the concepts of GET, PUT requests. I am currently working on an app where I display a list of users on one page, and on another page, I have a form with three buttons. My main focus is on the "Con ...

Move the element that can be dragged with the identifier X to the designated drop area with the identifier

I am attempting to create a function that will allow me to drag a draggable element and drop it into a designated container using their IDs. However, I am unsure of how to get started. dropToContainer("myDraggable", "div3"); function dropToContainer(co ...

Issues encountered when setting up a Context Provider in React using TypeScript

I am currently in the process of setting up a Cart context in my React TypeScript project, inspired by the implementation found here: https://github.com/AlexSegen/react-shopping-cart/blob/master/src/contexts/CartContext.js. I'm encountering some conf ...

What steps should I take to display a Material UI grid in React without triggering the warning about the missing unique key

In my current project, I am utilizing React and Material UI to display a grid. The grid's rows are populated from an array of data, with each row containing specific information in columns structured like this: <Grid container> {myData.map((re ...

Tips for customizing the background color and image of a toaster

Looking to modify the background color and image based on the else condition (toaster.error) success: function (data) { if (data.message != ""){ toastr.success(data.message); ...

Conduct a text search within mongoDB to retrieve the corresponding reference document for every item stored in the collection

I am in the process of developing a search functionality for a collection of trips. This search feature will check if the trip includes specific city names (origin and destination). Each trip entry contains the user ID of the person who created it. The sea ...

Guide to creating a parallax scrolling effect with a background image

My frustration levels have hit an all-time high after spending days attempting to troubleshoot why parallax scrolling isn't functioning for a single image on a website I'm developing. To give you some context, here's the code I've been ...

Turkish text is not appearing correctly on the UIWebview when embedded in HTML

One of the challenges I encountered in my iOS project involved programming a UIWebView to load content in both English and Turkish from a web service. To accomplish this, I formatted the HTML string with the necessary headers and saved it locally before pr ...