Angular 5 TypeScript's key value data structure

I am in search of a way to replicate the functionality of a key/value dictionary in Python. My project is utilizing Angular 5 typescript, and I am still exploring the available data types. The ever-changing nature of Angular has made it challenging due to certain datatypes becoming outdated or unsupported.

Here is my objective: If anyone could suggest a reliable resource for guidance, I would be extremely grateful.

Equivalent Code in Python

string = "the bump from the sump in the sump for the trump and the bump"


a = {}
for x in string.split():
    if x in a.keys():
        a[x] += 1
    else:
        a[x] = 1
print(a)

for b in a.keys():
    if a[b] > 1 and a[b] < 4:
        print(b)
    if a[b] >= 4:
        print(b)

Answer №1

To convert your code to TypeScript, follow the example below:

const sentence: string = "the bump from the sump in the sump for the trump and the bump";

const words: Array<string> = sentence.split(' ');

const wordCount: { [key: string]: number } = words.reduce((acc, word) => {
  acc[word] = (acc[word] || 0) + 1;
  return acc;
}, {});

Object.keys(wordCount).forEach((key) => {
  if (wordCount[key] > 1 && wordCount[key] < 4) {
    console.log(wordCount[key]);
  } else if (wordCount[key] >= 4) {
    console.log(wordCount[key]);
  }
});

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

The CORS policy is blocking Angular Socket.io Node.js because the requested resource does not have the 'Access-Control-Allow-Origin' header present

When trying to access the socket endpoint from my frontend, I encounter this error message: chat:1 Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NOAlAsz' from origin 'http://localhost:4200& ...

Angular Elements generates compact, single-line HTML output

It's incredibly frustrating how browsers handle inline-block elements, creating invisible margins when placed side by side. You can experience this "bug" firsthand here: http://jsfiddle.net/8o50engu/ Interestingly, if these elements are on the same l ...

I'm curious as to why I am receiving an empty array as a response when I send a post request to the API

I am experiencing an issue while sending data to an API using a post method. I have structured the data in an object as per the server's requirements, but upon posting the object to the API, I receive a response with a status of 200 and an empty array ...

Best practices for Rest API design: Enumerate potential values for entity attributes

Exploring the most effective method for populating dropdown lists in a UI with data and determining the optimal REST URI structure to support this process. Let's consider an entity called Car, which includes an attribute called "type". The type attr ...

Improving the clarity of Jest snapshot test logs using styled from MUI

Currently, I am working with MUI v5 along with styled components and Jest snapshot testing. I am looking for a way to improve the logs generated when a snapshot test fails. Below is an example of the component I am dealing with: const styledProperties = n ...

Transform this React-Redux function component into a class component

After following the guide provided on https://github.com/Microsoft/TypeScript-React-Starter to set up my project, I encountered an example with a react component as a function. This is the current code snippet: export interface Props { name: string; ...

Navigating the murky waters of Angular 2 component class methods: Should they be public or

I find it challenging to determine which methods should be designated as private and which should be public within a component class. It's generally straightforward in a service to determine whether a method should be public or private, for example: ...

Utilizing Angular 2 to Implement Remote Validation Error Handling in Forms

Whenever I submit a form to my backend, there is a chance that I will receive a response with HTTP code 400, which means that the validation has failed. An example of the response from my backend could be: { "status":"fail", "data":{ "email": ...

Angular 2 - Ensuring service executes only when boolean condition is met

I am currently dealing with a navigation menu that utilizes the ng2-page-scroll module. When scrolling through the page using hashtag links, I encountered an issue. If I navigate across routes, there is a delay in loading the data. As a result, the servic ...

Transferring an object to a different component through a router

I've created a todoDetail component like this: <mat-card class="todos" [class.done]="todo.done === true"> <h3> <input (change)="done(todo.id)" [checked]="todo.done" type="checkbox"/> <a [title]="todo.name + ' deta ...

The Angular HTML Autocomplete feature is not functioning as intended when hosted on a CDN in Chrome, despite setting it to "nope" or "off"

When setting Autocomplete to "nope" or "off" in my input box for surname, I'm still able to select from the autocomplete list. This issue occurs when accessing our Ui app through CDN at link [https:// Xyz. net], but works correctly when accessing it d ...

The error occurred due to a TypeError when trying to access undefined properties (specifically 'push') while the R3Injector was processing a provider at core.mjs on line 11577

While working on a website, I came across this error message when trying to navigate: ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'push') TypeError: Cannot read properties of undefined (reading &apo ...

Transitioning from angular version 5.2 to the latest angular 6.1 version

Currently, I find myself on node 8.11 and utilizing VS code. The project was originally built in version 5.2.5 but has been inactive for some time now. Within my project, the following modules are present: import { BrowserModule } from '@angular/plat ...

Is there a way to create identical copies of data with the identical names?

Here is the code I have written: this.temp1.push(this.json); for(let i=0; i<this.temp1.length; i++){ if(this.temp1[i].name == this.json.name){ this.orderList[i] = this.json; this.DBorder[i] = this.order_json; } ...

Guide on accessing an element from a predicate object in Typescript while using Angular

I'm trying to wrap my head around how to access an element that exists on an object of a specific type but is defined as a type predicate. For example, let's say we have a Team defined as: let team$: Observable<ErrorModel | Team> The res ...

Having issues with my Angular 4 + MVC web application not functioning properly in Internet Explorer

After creating an application using Angular 4 and MVC, I noticed a problem specifically with Internet Explorer. The application runs smoothly on Chrome, Firefox, and other browsers except for IE. Does anyone have any suggestions on how to resolve this br ...

What purpose does the "items" property serve in the MatMenu component of angular/material2?

The Angular material's MatMenu documentation is available at https://material.angular.io/components/menu/api. In this documentation, the API includes a "items" property that is documented as an attribute of type QueryList for the MatMenu component. A ...

Customizing the appearance of Indicators and Paginator in PrimeNG Carousel

I have integrated a carousel using PrimeNG which has the following design here Take note of the style of the carousel indicators and navigators. I want to achieve the default style of indicators/navigators for the carousel as shown here I have included t ...

Creating packages for multiple Typescript projects that rely on a shared local module

I am currently developing a series of VSTS extensions, with each extension being its own independent Node project complete with its own package.json file and node_modules folder. The structure of the folders is as follows: - MyExtension - package.json ...

Anticipated that 'styles' would comprise an array of strings in Angular2

Whenever I encounter this issue Uncaught Error: Expected 'styles' to be an array of strings. Below is my code example styleUrls:[ "../../styles/login.component.styles.scss" ], Interestingly, when I declare them in a similar manner but ...