How can you insert a title or key into an array containing numerous objects using javascript?

I have a function that extracts data from files and returns it in an array

const extractTestCases = () => {
    const filesArray = []
    const filterTestSuite = files.filter(test => test.includes('.test.ts'))
    filterTestSuite.forEach(testsuite => {
        const feature = regexMatcher(testsuite, featureRegex, 1)
        const testSuites = fs.readFileSync(testsuite, { encoding: "utf8" });
        const testCases = regexMatcher(testSuites, TestRegex, 1)
        filesArray.push({ [feature]: testCases })
    })
    return filesArray;
}

This currently gives me an output like the following

[
  {
    featureCustom: [ 'somethig' ]
  },
  {
    featureInsp: [
      'bla bla',
      'foo foo'
    ]
  }
]

I want to modify it to generate an output like this

[
    {
        "features": [
            {
                "featureCustom": [ 'somethig'
                ]
            },
            {
                "featureInsp": ['bla bla','foo foo'
                ]
            }
        ]
    }
]

Answer №1

Is it similar to this?...

function extractTestCases() {
    const testCases = [];
        testCases.push({ [feature]: testCases });
    return [
      {
        features: testCases
      }
    ];
}

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

Send both file and model data using AngularJS with FormData

When uploading an image using the file input type along with other user data, my model includes the User class: public class User { public int Id { get; set; } public string Name { get; set; } public int Rollno { get; set; } public byte[] ...

Generating a client-side MD5 hash for an image file in order to compare it to the hash calculated by Firebase?

Is there a way to calculate the MD5 of an image file on the client side within my Angular Map application, that will match the MD5 when I store the file on Firestore? I need to verify that a user's file matches a reference version stored in Firebase ...

Using express.static can cause an issue with a Nodejitsu application

I'm completely puzzled by this issue that keeps cropping up. Whenever I try to add a static path to my app, I encounter an error on the hosting platform I use called "nodejitsu". The error message indicates that the application is not functioning prop ...

When accessing nested objects in Props in Vue.js, an error may occur if the property of null/undefined

When I make an axios call, I receive an object which I pass to a child component. However, when I attempt to loop through the object in the child component to access a property of another nested object, I encounter an issue where the property is showing as ...

struggling with utilizing ajax for outputting data using PHP and POST variables

Currently, I am attempting to execute a PHP script that retrieves data from a database by simply clicking a button. My intention is to implement AJAX in order to prevent the page from refreshing. While testing with traditional post/submit methods and enc ...

How can animations be disabled in Angular/Javascript?

I have been assigned the task of developing an Angular component for my company's applications that will include a toggle to disable all animations within the app for accessibility purposes. It is important to note that I am unable to go into each in ...

Is there a way to store JSON data in a constant variable using Node Fetch without encountering the error TypeError [ERR_INVALID_URL]: Invalid URL?

In my current NodeJS project, I am working on extracting data from a JSON file and then sending it to a constant variable in my app2.mjs. The goal is to organize this data into an array of objects and eventually save it into a database. However, when tryin ...

Issue with Angular: Mobile view toggle button in the navbar is unresponsive

While the dropdowns are functioning correctly in web mode, the navbar toggle isn't working as expected in small screens or mobile mode. I've been trying to figure out the issue by referring to code from CodePen that I am learning from. Despite i ...

Leveraging jQuery within Node.js

Greetings, I am a newcomer here and I hope that my message is clear. My current challenge involves trying to incorporate jQuery into node.js, but unfortunately, all my efforts have ended in failure. I have spent hours attempting to resolve this issue to no ...

How can we transfer a jQuery value from an input field without using a single

This task may seem challenging. How can single quotes be eliminated from a variable when passed directly to another variable? I am using jQuery variable $("#test").val() to extract the value from an input below <input type="text" ...

What is the best way to access a document that has the lang attribute set to "en" using JavaScript?

I am working on implementing conditional styling in JavaScript by referencing this line of code located before the opening <head> and <body> tags: <html class="js" lang="en"> I want to set a condition so that if the l ...

What is the best way to update the state of object.item[n] that is nested within an array, which in turn is nested within

A unique data structure is set up in the following way: const library = [ { procedureName:'Build Foundations', id:1, tasks:[ { taskName:'dig at least 1m deep', isCompleted:false, procedureId:1 }, ...

Json mapping successfully pairs keys but struggles with accurately matching values

I have encountered an issue with my JSON mapping. The format works fine for the keys, but I am facing problems with the values - sometimes nothing is displayed or it shows NULL. If you have any insights on how to troubleshoot this, I would greatly apprecia ...

Provide props to vue-router along with boostrap-vue's b-nav-item

I am currently in the process of transitioning my SPA from modals that load components to routed pages that load components. I have been able to successfully open a page using to="/fixtures" in the html, but I am facing an issue with passing in a component ...

Exploring Request Parameters in SailsJS

I am facing an issue with logging requests in my Sails app. While I have managed to log the requests successfully, I am unable to log the parameters of the request. myRequestLogger: function(req, res, next) { if (sails.config.environment === & ...

The jQuery show/hide functionality is malfunctioning

In my PHP code, I have the following loop: foreach ($sid as $key => $value) { $sql = " a sql query "; $vehicle->rowQuery($sql); $numRows = $vehicle->rows; while ( $data = $vehicle->result->fetch_assoc()) { $ ...

Utilize CamelCase in jQuery for Better Code Readability

Upon examining the jQuery source code, I noticed an interesting use of camelcase: camelCase: function( string ) { return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase ); } // where: rmsPrefix = /^-ms-/, rdashAlpha = /-([\da- ...

Mirror the content of my div code onto a two-dimensional plane using an A-frame

Query I am currently developing a 3D scene using A-Frame () and I am in need of a solution to mirror an HTML div onto a plane within the A-Frame environment. For instance, imagine a scenario where there is a div at the bottom left corner of the screen fun ...

Can a class with explicitly implemented interface properties be converted to JSON format using serialization?

interface X { string Label {get;set;}} interface Y { string Title {get;set;}} class Z : X, Y { string X.Name {get;set;} string Y.Title {get;set;}} var z = new Z(); ((X)z).Label = "example"; ((Y)z).Title = "sample"; var result = JsonConvert.SerializeObj ...

Encountering the error "ReferenceError: __extends is not defined" is a common issue when modifying the rollup.config.js commonjs function in projects that use the ReactJS library

Currently, I am involved in a project and there is also a library project containing all the common components used throughout. Within this library, I had to integrate a component that relies on materialUI. However, upon trying to export this component, I ...