Having difficulty retrieving objects within a foreach loop of object keys that do not meet the criteria of the string.prototype.replace method

Currently, I am working on looping over objects within a key:value array in JavaScript to use the string.prototype.replace method for paths to JS files in GulpJS concat tasks.

The objective is to generate a list of paths that GULP can utilize, but they require processing before that. I am replacing the 'placeholder' values in the paths array with their corresponding substitution values from the subs array through a loop until all paths are converted to literal form.

An issue arises in identifying the paths that remain unprocessed by the loop, such as '../test4' (literal paths), so I can handle them separately after completion.

This is the initial progress with some sample data for demonstration:

function updatePaths(substitutionsArray, pathArray) {
    var updatedPaths = [];

    pathArray.forEach(function (path, index, array) {
        console.log('Processing line ' + (index + 1) + '... ' + path);
        Object.keys(substitutionsArray).forEach(function (placeholder) {
                var substitution = substitutionsArray[placeholder];

                var fixedPath = path.replace(placeholder, substitution);

                if (fixedPath == path) {
                    console.log('No replacement found in path ' + path + ' for placeholder ' + placeholder);
                } else {
                    console.log('Replacement discovered in path ' + path + ' for placeholder ' + placeholder + ', resulting in ' + fixedPath);                    

                        updatedPaths.push(fixedPath);
                }
            }
        )
    });
    console.log(updatedPaths);
    return updatedPaths;
}

var substitutions = {
    "@COMMON_JS@": "/test1",
    "@COMMON_ROOT@": "/test2",
    "@COMPONENT_ROOT@": "/test3",
    "@COMPONENT_WEB_ROOT@": "/components"
};
var filepaths = ['@COMMON_JS@/test1', '@COMMON_ROOT@/test2', '@COMPONENT_ROOT@/test3', '../test4', '../../test5'];

output = updatePaths(substitutions, filepaths);

console.log(output);

return;

If you have any recommendations on how to approach this task more effectively, please let me know.

Thanks in advance - Dave

Answer №1

The paths cannot be retrieved from the console. When using Console.log, it always returns undefined. If you wish to display the value you pass in, you can create a function that logs it like this:

function log() { console.log.apply(console, arguments) return arguments }

This function will return the arguments you input into it. More information about the arguments object can be found here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

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

Is there a way to remove a certain child category post from appearing in a parent category?

I'm having trouble with displaying related posts by category while excluding a specific category. I've tried different methods but none seem to work, and I'm not sure how else to approach this issue. <?php $categories = get_the_terms ...

Node.js server experiences a crash after attempting to send a large string using res.send()

I've recently started learning JavaScript and NodeJs. I'm facing an issue with my Nodejs application crashing when a get request is made, specifically when trying to return a large string. app.mjs app.get('/log', function (req, res) { ...

Investigate issues with POST data requests

I recently utilized a REST API to send a POST request. Upon clicking on the function addmode(), a textbox is displayed allowing users to input data. However, upon clicking the save() button, an unexpected error occurs and redirects to a PUT Request. Using ...

Understanding the extent of testing coverage in a project using karma and webpack

For my project, I am incorporating ES6 syntax and testing my code with Karma. While I have successfully set up testing, I encountered an issue with the coverage report. Instead of including the source code, the coverage report is highlighting spec file ...

Vow failing to function as intended

I have encountered an issue with performing a nested query in MySql, saving the result to a variable, and sending it over http. The problem is that the program always executes console.log("test 2:"+rpsData); before the query finishes. Despite attempting ...

Is JQuery the ultimate solution for creating a dynamic multi-language website?

Embarking on a new project that requires support for multiple languages. My plan is to create a jQuery/AJAX based application with all the code in jQuery, simply calling JSONs for data. What would be the most effective approach for implementing multi-lan ...

Show only the selected option with jQuery's on change event and disable or remove the other options

My goal is to make it so that when a user selects an option from a dropdown menu, the other options are disabled or hidden. For example, if option "1" is selected, options "2", "3", and "4" will be removed: <div class="abc"> <div class="xyz"> ...

Warning: The Unhandled Promise Rejection arises when an error is thrown within an asynchronous function without a try-catch block

Encountering the following issue in my Node-Express App UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error came about either by throwing inside an async function without a catch block, or rejecting a promise that was not hand ...

Having trouble getting getStaticProps to display JSX in Next.JS

I'm currently facing an issue with rendering basic data from a locally hosted Strapi API in my Next.js project. Although the data is successfully logged in the console, I am unable to map it into JSX. Below is the API get function: export async func ...

Seeking a solution for inserting input values into a JSON file within a node.js environment

As I was developing my new project, a to-do list web application, Below is the code snippet from 'todo.html' : <html> <head> <title>My TODO List</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery ...

jQuery Dialog interface endlessly scrolls to the top

While debugging code for a project, I came across the use of jquery UI Dialog for popups. However, there seems to be an issue where the page keeps scrolling to the top while the dialog remains stationary wherever it was opened. Below is the code snippet in ...

Displaying events in the all-day section can be achieved by setting the defaultView of fullCalendar.js to 'agendaDay'

Trying to use fullCalendar.js with defaultView set to agendaDay, and pulling events from mysql as JSON. However, I'm encountering an ERROR where it displays the events in the 'all-Day' section. Here are some of the solutions I attempted: e ...

I possess a dataset and desire to correlate each element to different elements

[ { "clauseId": 1, "clauseName": "cover", "texts": [ { "textId": 1, "text": "hello" } ] }, { "clauseId": 3, "clauseName": "xyz", "te ...

What strategies can I implement to integrate Cordova with a combination of Meteor and React?

I'm currently struggling to implement a Cordova plugin with Meteor and React. According to the documentation: You should wrap any functionality that relies on a Cordova plugin inside a Meteor.startup() block to ensure that the plugin has been fully ...

calendar input type for both internet explorer and firefox

I frequently utilize the input type calendar on my website's intranet, but unfortunately, it only seems to work properly in Google Chrome and not in other browsers. As a solution, I have developed a code for a CSS/JavaScript calendar that I would like ...

Redirecting the socket.io client to the Heroku service

Recently, I developed a real-time chat application using socket.io, Node.JS, and express. It was functional on my local server but now I want to connect it to an existing Heroku service instead. How can I achieve this? Once I tried the following code, va ...

What is the best way to handle a large number of nested AJAX GET requests?

My task involves making numerous AJAX GET requests, which must be nested because each request depends on variables from the response of the previous one. Although I was able to make multiple requests with the example below, it becomes impractical when dea ...

How can I call the telerik radgrid.databind() function using a JavaScript function?

Currently, I am coding in ASP .NET and have an ASPX page featuring a Telerik RadGrid. I am curious to know if it is feasible to call the RadGrid.DataBind() method from within a JavaScript function? ...

What exactly is the purpose of editing a host file?

After reviewing this repository, an automatic message pops up: Don't forget to modify your host file 127.0.0.1 * http://localhost:3001 What exactly does that entail? ...

Easy way to eliminate empty elements following a class using jQuery

I'm encountering a situation where I have a group of elements following a specific class that are either empty or contain only white space. <div class="post-content"> <div class="slider-1-smart"> --- slider contents --- < ...