Change the keys of the object in the return statement

Scenario

Imagine a scenario where a method called matter is returning an object in the form of return {content, data}

Issue

There is a conflict when the method is called a second time, as it overwrites any previous variables that were set from the return.

import matter from 'gray-matter'

const test = () => {
...
   const { content, data } = matter(source1)
   const { content, data } = matter(source2) // this causes the previous content and data variables to be overwritten
...
}

Objective / Inquiry

The goal is to set the return values in different named variables, for example:

const { content2, data2 } = matter(source2) // however, this results in a compile error as property content2 does not exist on type [...]

Therefore, the question remains; how can the return values be assigned to different named variables that match their type?

Answer №1

To avoid confusion, try using unique variable names:

const { content, data } = matter(file1)
const { content: body, data: info } = matter(file2)

If destructuring is not necessary:

const result1 = matter(file1)
const result2 = matter(file2)

Another option is to utilize an array of objects:

const files = [file1, file2].map(matter);

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 feature of automatically selecting all text input content upon focus is not functioning properly in Microsoft Edge

I've encountered a specific issue with Microsoft Edge when trying to select all the text in an input field. My approach involves using Angular's ng-focus to trigger a function in the controller that selects the text in the field. function select ...

What is the best way to combine cells within a single column?

Exploring the code snippet utilizing ag-grid with Vue 3. <script setup lang="ts"> import "ag-grid-community/dist/styles/ag-grid.css"; import "ag-grid-community/dist/styles/ag-theme-alpine.css"; import { AgGridVue } from ...

Tips for structuring an object map with flow type to ensure that each value includes a corresponding key

In an attempt to correctly type (using Flow) a helper function called createReducer for Redux, I have utilized the code from redux-immutablejs as my starting point. I am following the recommendations from the flow documentation on typing Redux reducers, b ...

Vercel encountered issues with "validating code quality and type correctness" during deployment but was successful when performed locally

Running "next build" locally and "vercel build" both work smoothly. However, once deployed to vercel, the "Linting and checking validity of types" fails during the build process. It seems like TypeScript is stricter when building on vercel even with the sa ...

Exploring the capabilities of mongojs: Adding items to arrays within fields

I've been researching extensively on this subject, but I'm completely stumped. Here's my dilemma (I'm working with node.js and mongojs): I want to create documents like the following: { "_id" : ObjectId("50ce2f7f98fa09b20d000001" ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...

Testing React components within a controlled environment using cypress-react-unit-test

I'm currently facing a challenge in comprehending how to modify the props of a react component while utilizing cypress-react-unit-test. Below is a straightforward controlled input component: interface MyInputProps { inputVal: string onInputCh ...

Transforming the FormData() format into a jQuery structure

Hey there! I've been doing some research online about uploading files using JavaScript, and I stumbled upon some great resources, including this one https://developer.mozilla.org/en-US/docs/Web/API/FormData. I have a script that uploads an image to th ...

The 'mergeMap' property is not found on the 'Observable<any>' type

Currently, I'm working on an HttpInterceptor within my Ionic 4 application. My goal is to retrieve the Bearer Authorization token stored in local storage. Although I attempted to utilize mergeMap for this task, I kept encountering the following error ...

Can InstanceType<T> be utilized with an abstract class?

My intention is to enable it to reference implementations rather than the abstract itself, all while exclusively depending on the definitions in the abstract interface. ...

The API has been triggered twice

I am currently working on a React component where I have implemented an API call using the useEffect hook with an empty dependency array. However, I noticed that the API is being called twice and I can't seem to find the reason behind it. import { use ...

Measuring Load Time Percentage in AngularJS Using $http Requests

I am currently utilizing Angular with my Rails application. I have a sample app and I am interested in displaying the response time required to load a response in Angular. For example, When loading a response of an array containing 100,000 elements, I w ...

What causes the intersection observer handler to not trigger when using scrollTo or scrollIntoView?

When an intersection observer is added to an element, it triggers the handler function when scrolled past a specific point. However, if the element is scrolled past that point using a button click, the handler does not get triggered. Is there a way to man ...

Different options for reading large files during an AJAX file upload

It seems that php's file_get_contents is not able to handle large files. What can be done as an alternative if it is causing a "stack overflow" issue? HTML File <form id='test'> <input type='file' name='Receipt&ap ...

Definition of DataTypes in TypeScript

My goal seems simple to me, but I am realizing that my experience with Typescript might not be enough. I want to create a type that can accept the following expressions: const dp: DataPoint = [1, 2]; const dp2: DataPoint = [1, 2, 3]; const dps: DataPoints ...

Using jQuery .sortable() to reorder list items and update their IDs based on their new positions

My apologies for my limited English skills, but here is what I am trying to achieve. I have a list structured like this: <ul id="sortable" class="sortable"> <li id="1">1</li> <li id="2">2</li> <li id="3">3</li> &l ...

Attempting to utilize JSON.Stringify on Scripting.Dictionary objects will result in failure

In my current ASP classic project, I have integrated the JScript JSON class available at this link. This class can interact with both VBScript and JScript, closely resembling the code provided on json.org. However, due to team manager's requirement, I ...

Implementing change event to activate select dropdown with jQuery

Is there a way to modify the code so that select2 and select3 are disabled by default, but become enabled only when an option is selected in the previous select dropdown (excluding the placeholder "Select your option")? In addition, I want the first place ...

Difficulty encountered when applying date filtering on a specific filter in the MUI-DataGrid

Software Version: "@mui/x-data-grid": "^6.2.1" I have a script that generates columns for the DataGrid as shown below (only including relevant code) if (prop === "date" || prop === "dateModified" || prop === "n ...

managing nested JSON arrays in JavaScript

I have a straightforward task with handling a simple array that is divided into two parts: a group of vid_ids and a single element named page. Initially, I was iterating through the vid_id array using a for loop. However, upon adding the page element, I en ...