Reversing the order of items in Angular 2 for repetition

Is there a way to display search items in reverse order? This is what I have attempted so far:

 let newSearchTerm = this.getItem(this.searchHistoryKey)
      newSearchTerm.push({
        'q': this.searchTerm
      });

 this.setItem(this.searchHistoryKey, JSON.stringify(newSearchTerm));




<li *ngFor="let search of searchHistory"><span th-icon th-time>icon</span><a href="/"><label for="">{{search.q}}</label></a><span (click)="addHistory()" th-icon arrow-up>icon</span></li>

Any suggestions on how to display the search results in reverse order?

Answer №1

One potential solution is to utilize the array.reverse() method:

this.setItem(this.searchTermKey, JSON.stringify(updatedSearchTerm).reverse());

Answer №2

Give this filter a shot:

app.filter('reverse', function() {
  return function(items) {
    return items.slice().reverse();
  };
});

...
...
...

*ngFor="let search of searchHistory | reverse"

Answer №3

reverseArray(jsonArray) {
      let reversedArray = [];
      for (let index = jsonArray.length - 1; index >= 0; index--) {
        reversedArray.push(jsonArray[index]);
      }
      return reversedArray;
    }

This solution was very useful, and I implemented it using only plain javascript.

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

Efficient ways to manage dropdown cells in ReactGrid

Is there a way to assign individual values to each select element in a cell? I am using ReactGrid with react version 16 There seems to be an issue with the onchange function, and I'm struggling to find help import * as React from "react"; ...

Fixed headers remain in place as you scroll horizontally on a single table

Within my system, I have organized two tables: table one and table 2 are stacked on top of each other. I've managed to freeze the column headers so that they are displayed vertically, remaining static as users scroll horizontally. To achieve this effe ...

Obtaining the ViewRef of the current component in Angular 4

How can I obtain the ViewRef for my current component? I am attempting to retrieve the ViewRef from a service. Below is the code: component.service.ts import { Injectable, ViewRef } from '@angular/core'; @Injectable() export class CheckboxSe ...

Transforming Numbers into Strings with Angular 2 Using Pipes

Need help converting a number value to a string in Angular 2 using TypeScript within a pipe. I'm getting this error message: Type string is not assignable to type number Here is my pipe code: import { Pipe, PipeTransform } from '@angular/cor ...

establishing the default value as p-multiselect

Here is the code snippet I am currently working on: export class LkBoardStatus { id : number = 0; descr : string = ''; } In the component.ts file, I have defined the following: //... lkBoardStatusList: LkBoardStatus[] = []; selectedStat ...

The list in Ionic 3 search does not appear after clearing the search bar

Can anyone assist me with my search bar issue? I'm able to display words on the list, but once I clear the search bar, nothing shows up. Here is a snippet of my code: In my home.html file: <ion-searchbar (ionInput)="getItems($event)" [showCancelB ...

What is the best way to create a generic function parameter for a single property of an object?

I am trying to refactor a generic function into accepting parameters as a single object function test<T>(a: string, b: T, c: number) Instead, I want the function to receive an object like this: function test(params: {a: string; b: T, c: number}) I ...

Tips for incorporating nested generics in Typescript

Currently, I am developing a straightforward activity execution framework that allows developers to define activities which can be executed within a workflow. To enhance type safety and boost developer productivity by utilizing type hints, I aim to incorp ...

Explore the refined search capabilities within the ng-select dropdown component in Angular

I am currently utilizing the ng-select library for a list of cities, such as: cities = [ {id: 1, name: 'MA, Boston'}, {id: 2, name: 'FL, Miami'}, {id: 3, name: 'NY, New York', disabled: true}, ...

Validating dynamic textboxes in Angular with custom rules

Creating dynamic textboxes with *ngFor in Angular <tr *ngFor="let computer in _Computers; let i = index;"> <td>{{computer.Name}}</td><td>{{computer.Optional}}</td> <td> <input matInput [formControl] = " ...

Running Jenkins in a Docker container to initiate the process of building an Angular application within a

Recently, I began my journey of learning Jenkins and successfully launched it on Docker. Now, I have a project built in Angular along with a Dockerfile created to produce a Docker image. However, when attempting to start the process from Jenkins, an erro ...

Angular - Automatically filling in an empty input field upon dropdown selection

My goal is to create a DropdownBox that will automatically fill input fields based on the selected value. For example, selecting "Arnold" from the dropdown will populate another textbox with "Laptop". How can I accomplish this? { name:'Arnold', i ...

a guide to transforming data into a string with json using angular

I am struggling to figure out how to bind my form data into a JSON string. My situation involves using a string field in the database and saving checkbox values in a single database column using JSON. I have created an HTML form, but I am unsure of how to ...

Guide to incorporating third-party libraries in Angular

Greetings, I have a common question regarding Angular and utilizing third-party libraries. As someone who does not frequently work with Typescript/Frontends, I am encountering an issue with Angular. Specifically, I am attempting to incorporate the node-htm ...

Search for records in MySQL using Typeorm with the condition "column like %var%" to retrieve results containing the specified

Looking for a way to search in MySql using typeorm with the "column like" functionality. async findAll({ page, count, ...where }: CategorySelectFilter): Promise<Category[]> { return this.categoryRepository.find({ where, ...

Can you explain the distinction among the various FormControl initialization techniques?

Can you discern the variance among the following methods: input: new FormControl({}, inputValidatorFn()) input: [{}, inputValidatorFn()] input: formBuilder.control({}, inputValidatorFn()) They appear to function identically. Is there truly no disparit ...

obtaining the value of an input using typescript (put request)

Does anyone know how to extract input values and store them as JSON? I'm having trouble with accessing the input value in this scenario. When I attempt document.querySelector("todo-text").value, it results in an error. const NewTodo: React.FC<NewT ...

What is the process for creating a method within a class?

Here is the current structure of my class: export class Patient { constructor(public id: number, public name: string, public location: string, public bedId: number, public severity: string, public trajectory: number, public vitalSigns: ...

I encountered a conflict with the FOREIGN KEY constraint while attempting to execute the INSERT statement in the frontend application. However, the same statement successfully runs

I am currently developing a project using Angular for the frontend (Visual Studio Code) and .NET Core for the backend (Visual Studio). Everything seems to be functioning perfectly when I test the backend endpoints with Postman. However, I encountered an i ...

The disappearance of UI elements in Angular 13 and Bootstrap 5 when new routes are introduced

After spending a considerable amount of time on website development, I have hit a roadblock with the navigation. Whenever I set up a route, the entire user interface disappears and refuses to load. I have searched extensively but found no solution to this ...