Removing curly braces and double quotes from an array object can be achieved by iterating through each element

Within this particular project, I have utilized Angular 8 and TypeScript.

In the implementation, there exists an array that showcases emails either through user input or via CSV upload. Once the user inputs several emails, a button is available to send invitations to these email addresses.

To populate the emails into the array, I employ the following code snippet (where "this.emails" serves as the identifier for the array defined as emails: string[] = []; and accessed through an ngfor loop in the HTML).

 onFileInput($event: any): void {
    const files = $event.target.files as File;

    this.csvService
      .parse(files[0], { header: false })
      .pipe()
      .subscribe((result: Array<any>) => {
        this.emails = result;
      });
  }

Though this method successfully adds emails to the array, a peculiar issue arises when using the invite function. The email address within the array is identified as {"[email protected]"} instead of just [email protected].

A surprising observation is that the array actually displays [email protected]. On the other hand, manual entry correctly identifies it as [email protected].

The root of the issue seems to lie in the format of what is loaded into the array with the provided code - specifically, it is not a plain string.

Upon logging, a CSV-imported email appears as

email: Array [ "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbded6dad2d7fbcfdec8cf95dfde">[email protected]</a>" ]
, whereas a typed input email is logged as
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedbd3dfd7d2fecadbcdca90dadb">[email protected]</a>"
(which is correct).

Hence, is there a way to adjust the function to eliminate the curly braces and quotes before entering the array?

Answer №1

If you want to clean up text by removing curly braces {} and square brackets [], you can use regExpr.

this.csvService
  .parse(files[0], { header: false })
  .pipe(
     map((emails:any[])=>
         emails.map((email:string)=>email.match(/([\[{]).+([\]}])/)?
                      email.match(/([\[{]).+([\]}])/)[0].slice(1,-1):
                      email
       )
     )
  )

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

Debugging the Force-Directed D3 Graph

I stumbled upon a fantastic article that provided a detailed guide on creating a beautiful D3 force layout graph. However, I'm facing some difficulties with the JSON source: The "links" attribute in the author's JSON doesn't seem clear to m ...

How can I use "Lite-Server" with NPM start to showcase my index.cshtml file on the browser?

Currently, I am trying to navigate the world of Visual Studio Code and figure out how to run/compile my project. Starting a new project in Visual Studio was simple enough, but now that I'm working with Visual Studio Code, I find myself struggling to s ...

What is the best way to fetch all Firebase database IDs using Angular?

Is there a way to fetch all data from Firebase database along with their respective IDs? Currently, I have two functions - getAll() and get(input) that retrieve specific products based on the given ID. However, my current implementation only returns obje ...

What is causing my PHP multi-dimensional array to malfunction?

I have a multi-dimensional array that is functioning properly. However, I am facing an issue while trying to utilize the explode or in_array functions to limit the array when calling via $_GET <? $shop = array( array("red", "black", "blue", "green"), a ...

Tips for effectively parsing extensive nested JSON structures?

JSON Data on PasteBin I need assistance in converting this JSON data into an Object. It seems valid according to jsonlint, but I'm encountering issues with parsing it. Any help would be greatly appreciated. "Data":[{...},{...},] // structured like t ...

The change event for the select element is malfunctioning

Currently, I am deep diving into Nodejs with the second edition of the node cookbook. This book has caught my attention because it explains concepts using practical sample code, making it easier to grasp. The example code I am working on is related to Br ...

I am currently working on creating a system for displaying rating stars for products. The rating value, which ranges from 0 to 5, has already been provided

class PostBodyRatingStars extends React.Component { render() { const { classes } = this.props var rating = this.props.rating function createMappingArray(rating) { var arr = [] for (i = 0; i < 5; i++) { if (rating >= ...

How can I insert my URL into the webPDFLoader feature of LangChain platform?

I need help figuring out how to load a PDF from a URL using the webPDFLoader. Can someone explain how to implement this? Any assistance would be greatly appreciated. I am working on this task in nextjs. Where should I place the pdfUrl variable within the ...

When the time comes, ReactDOM will render your element into the designated container,

What does the [,callback] parameter represent in the ReactDOM.render(element, container) method? ...

The Vue 2 watch feature may encounter difficulties with mixin data properties, however, it functions properly when the data property belongs to the

I recently attempted to refactor some code by moving certain functionalities to a vue mixin from the component itself, with the intention of reusing it in multiple components. The simplified version of the code looks like this: export default { data() { ...

Include the model.obj file into the three.MESH framework

I am a beginner in three.js and I am developing an augmented reality application on the web to showcase plates of food. Currently, I have successfully displayed a cube. However, when I attempted to display a model.obj instead of using geometry and material ...

Issue with retrieving JSON data in chart object: Error reading property 'length' of undefined in Angular/ng2-charts

     I have successfully retrieved JSON data in the following format:    [2193,3635,8417,0] The data is coming from localhost:8080. I aim to utilize this dataset for displaying a pie chart. Below is the code snippet from one.html: <div> ...

Encountering an error when accessing dynamically routed pages in Next JS

When I click on a link within the web app to navigate to a dynamically routed page for a product (http://localhost/1), everything works as intended. However, if I manually input a specific product number in the search bar to navigate directly to that page ...

Distributing JWT to the recipient using Express

Allow me to provide you with an overview of my application, which is quite straightforward. The main concept revolves around a user inputting their accountname and password into an html form on the /Login page like so: <form action="/Login" m ...

What is the best way to invoke the first exported function from the second exported function?

I am looking to create a file containing four or five exported functions. exports.firstFunction = function() { // some code }; exports.secondFunction = function() { // need to call firstFunction }; My issue is that I want the second expo ...

Making several fadeToggle functions with the same class name operate independently from each other

Trying to create a profile with a fadeToggle effect that activates when clicking on a trigger/button. The issue is, it works perfectly for the first profile on the page, but all subsequent profiles do not respond to the fadeToggle effect. Unfortunately, du ...

Optimizing Your HTML/CSS/JavaScript Project: Key Strategies for Modular

When it comes to web frontend projects (html/css/javascript), they are often perceived as more complex to read and maintain compared to Java/C#/C/C++ projects. Is it possible to outline some optimal strategies for enhancing the readability, modularizatio ...

What is the reason for using 'app' as the top-level directory name in React Native import statements within a project setting?

Seeking to comprehend the imports within React Native source code, specifically in a file named questionnaire.actions.js, relative to the top-level directory called lucy-app: ./src/containers/newUserOnboarding/questionnaire/questionnaire.actions.js The m ...

Is it possible to transfer the reactivity of a Vue ref to another ref while reassigning it?

Below is a simplified version of my Vue component: <template> <div @click="loadEvents">{{ loading }}</div> </template> <script setup> import { ref } from 'vue' let loading = ref(false) loadEvents() func ...

Waiting for a response from an API with the help of nodejs

I'm new to exploring Node.js and I'm interested in making API calls where the result is awaited before proceeding with any further actions. // defining endpoint function function getListMarket() { var deferred = Q.defer(); deferred.resolve(Q ...