ESLint Alert: Modifying a property of a function parameter

Suppose we have a data structure as follows:

products: {
  '123': {
     details: [
        {
           price: 45,
           unitPrice: 0,
           productName: 'apples'
        }
     ]
  }
}

Now, consider the following function:

function updateApplePrice(coefficient) {
    const products = cloneDeep(vehicleProductSelector.getAllProducts(store.getStore().getState()));
    let appleProduct;

    Object.keys(products).forEach((productId) => {
        const {
            details
        } = vehicleProducts[productId];
        details.forEach((detail) => {
            const {
                price,
                productName
            } = detail;
            if (productName === 'apples') {
                detail.unitPrice = coefficient * detail.price;
                appleProduct = products[productId];
            }
        });
    });
    return appleProduct;
}

The issue I am encountering is a linting error:

Assignment to property of function parameter

How can this issue be resolved without turning off the linting rule?

Many suggest using array destructuring to solve this problem, but it seems quite challenging given the complexity of this particular data structure.

Answer №1

Try utilizing map instead of forEach and treat function arguments as if they are immutable. It's puzzling to see you using

details.forEach((detail) => {...});
followed by modifying detail with
detail.unitPrice = coefficient * detail.price;
.

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

What is the best way to retrieve the name value from a nested JSON object using PHP?

Data: {"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:1 ...

RadAjaxNamespace is not found within the codebase

While testing the application on my development machine, I encountered an error message in the browser debug console stating "RadAjaxNamespace is not defined". Interestingly, this error does not appear when accessing the production server. Upon comparing t ...

Route Angular 4 application static resources to a /PORT directory rather than the primary domain

Our Angular 4 application is integrated within a Node + Express app and started like a typical node app, such as: node index.js On a local machine, the Angular app is served from the /client directory: app.use(express.static(__dirname + "/client")); Onc ...

creating styles for css elements using equations

I am curious about defining CSS element dimensions before they are rendered without using offset and jQuery. Is it possible to echo PHP into the width/height or position values? For example: <?php $width = 290px; $altwidth = 290; ?> <style> ...

Leveraging the package.json file for client-side packages, enabling them to be dynamically loaded in the browser

Considering expanding the structure of package.json to incorporate dynamic package (plugin) loading on the client side. I am curious about how this idea aligns with the vision of npm. Essentially, I am interested in loading multiple modules with shared met ...

Tips for inserting query outcomes into a fresh array:

Below is the query I created to retrieve all IDs from the user table that meet a certain condition. How can I store these results in a new array? $sql_req = $wpdb->get_results(" SELECT user.ID FROM user WHERE class = 'TEST' "); I came across ...

What prevents variables defined in outer blocks from being accessed by nested describe() blocks?

In my real code, I encountered a problem that I wanted to demonstrate with a simple example. The code below functions properly. I defined a variable in the root describe() block that can be accessed in the it() blocks of nested describe()s. describe(&apo ...

Presenting a ui-router modal while maintaining the parent state's refresh-free appearance

I have implemented a unique feature in my angular app called modalStateProvider. It allows me to easily have modals with their own URLs. // Implementing the modalStateProvider app.provider('modalState', [ '$stateProvider', function ...

Accessing ASP.net web services using JavaScript client

I have a client-server application that was developed by someone else, with the client side using JavaScript. I now need to create a new web service in ASP.net, but I'm unsure about how to call web methods. This situation is similar to what's des ...

Information sent to a slot does not trigger a response

Trying to create a mobile-friendly chat app, I want to hide the new message button during conversations and use a different button on desktop. In addition, I have a dialog for creating new chat groups. <v-dialog transition="dialog-top-transition&qu ...

Locating conversation within a block of text using Javascript

Is there a way to extract dialogue, the content between quotes, from a paragraph in javascript and save it in an array? var myParagraph = ' “Of course I’ll go Kate. You should get back to bed. Would you like some Nyquil or Tylenol?” “Nyquil, ...

What sets static array declaration apart from pointer array declaration when it comes to returning an array from a function in C?

In this scenario, CODE A executes without any errors while CODE B generates an error indicating an assignment to an expression of array type. What is the actual difference between CODE A and CODE B? The function input_array takes an integer n as a paramet ...

What is the best way to implement the hamburger-react-menu exclusively for mobile or small screen users?

Looking to implement a mobile menu in my component, specifically for mobile devices. The menu should display options like Home, Furniture Chair... when clicked on the hamburger icon. I have the hamburger-react-menu package installed but unsure whether to u ...

Node.js fails to acknowledge Ajax requests

I am creating a secure webpage specifically for an Ajax request. Below is the code for the simple request: window.onload = loaded; function loaded(){ var xhr = new XMLHttpRequest(); xhr.open('GET', '/data_annotations', true); ...

What is the process of transforming a jQuery load method into native JavaScript, without using any additional libraries?

Recently, I successfully implemented this ajax functionality using jQuery: $(function(){ $('#post-list a').click(function(e){ var url = $(this).attr('href'); $('#ajax-div').load(url+ " #post"); e.preventDefaul ...

What is the best way to create a deep clone of an XMLDocument Object using Javascript?

I am currently working on a project that involves parsing an XML file into an XMLDocument object using the browser's implementation of an XML parser, like this: new DOMParser().parseFromString(text,"text/xml"); However, I have encountered a situatio ...

Is it better to utilize a sizable JavaScript file or opt for a compact one?

I recently created a JavaScript file with over 6000 lines of code for various sections of my website. I'm debating whether to keep it as one large file or break it up into smaller parts and call them in their respective sections. What do you think? ...

What could be the reason for my div being draggable on a smaller screen, yet becoming unresponsive to dragging when viewed on a larger screen during inspection?

The positioning of the div also changes when viewed on a larger screen. When inspected on a bigger screen, the position of the div shifts completely. Check out the jsfiddle here: https://jsfiddle.net/annahisenberg/ft10ersb/20/ To view it in full screen m ...

Having trouble with html2canvas capturing pseudo elements?

I'm currently working on saving an image of a div that has a pseudo element. However, I've discovered that html2canvas does not support pseudo elements. Is there another library available to save a div as an image? I am utilizing the following ...

What is the best way to determine if a radio button has been chosen, and proceed to the next radio button to display varied information?

The goal is to display the <div class="resp"> below each radio button when it is selected, with different content in each <div class="resp">. The previously selected <div class="resp"> should be hidden when a new radio button is chosen. O ...