"Using Vue to compute the sum or calculate values within an array - a step-by

Having a data array like this

"item_tabel": [
  {
    "method": {
      "select_method": 6,
      
    },
    "innovation": {
      "select_innovation": 2,
    },
  }
],

How do I calculate the sum?

This is my computed method:

subtotalRow() {
    return this.$store.state.item_tabel.map((item,i) => {
      return Number(item.method.select_method * item.innovation.select_innovation)
      //how to sum (item.method.select_method + item.innovation.select_innovation)
     });
 },

An example in my table:

No | method | inov | total
1  |    6   |  2   | (6+2 = 8) 
2  |    2   |  2   |  4        

If using the * operator works and if using + doesn't work.

Thanks!

Answer №1

If you are determined to combine the numbers 6 and 2

The solution may be simpler than you think - just follow the steps outlined in your comment:
subtotalRow() {
    return this.$store.state.item_tabel.map(
        (item,i) => (item.method.select_method + item.innovation.select_innovation)
    );
 },

Answer №2

Here's a solution for you:

In order to use Array.map(), you need to pass in a callback function enclosed in braces '{}'

calculateTotal() {
    return this.$store.state.items_list.map(
        (item, index) => {item.method.selected_method + item.innovation.selected_innovation}
    );
 },

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

Waiting for the code to execute once the filtering process is completed in Next.js using Javascript

I'm seeking a way to ensure that my code waits for the completion of my filter function before proceeding. The issue arises because my filter function, which incorporates another function called useLocalCompare, causes a delay in execution. This delay ...

Tips for including a new class in an HTML element

My goal is to add a specific class to an HTML tag, if that tag exists. This is the code I have attempted: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>index</title> <style> ...

Is there a benefit to using the Angular LocalStorageModule alongside angular-cache?

To configure the angular-cache, follow this setup: app.service('myService', function ($angularCacheFactory) { // This cache will synchronize with localStorage if available. Upon each app load, it will attempt to retrieve any previously save ...

Ending asynchronous tasks running concurrently

Currently, I am attempting to iterate through an array of objects using a foreach loop. For each object, I would like to invoke a function that makes a request to fetch a file and then unzips it with zlib, but this needs to be done one at a time due to the ...

Having trouble connecting to JSTL in my JavaScript file

Currently, I am facing an issue with my JSTL code that is housed within a JavaScript file being included in my JSP page. The problem arises when I place the JSTL code inside a script within the JSP page - it works perfectly fine. However, if I move the s ...

Enhance your images with the Tiptap extension for customizable captions

click here for image description I am looking to include an image along with an editable caption using the tiptap extension Check out this link for more information I found a great example with ProseMirror, but I'm wondering if it's possible ...

The URL in an AJAX request includes a repeating fragment due to a variable being passed within it

const templatePath = envVars.rootTemplateFolder + $(this).attr('link'); console.log("templatePath : ", templatePath); $.ajax({ method: "GET", url: templatePath, context: this, success : function(result){ ...

How does SWR affect React state changes and component re-rendering?

I am currently utilizing SWR for data fetching as outlined in the documentation: function App () { const [pageIndex, setPageIndex] = useState(0); // The API URL incorporates the page index, which is a React state. const { data } = useSWR(`/api/data? ...

The process of querying two MySQL tables simultaneously in a Node.js environment using Express

My goal is to display both the article and comments when a user clicks on a post. However, I've encountered an issue where only the post loads without the accompanying comments. Here is the code snippet that I've been working with: router.get(&a ...

Using JavaScript to open a new window and display CSS while it loads

I am looking to utilize a new window for printing a portion of HTML content. var cssLink = document.getElementByTagName('link')[2]; var prtContent = document.getElementById('print_body'); var WinPrint = window.open(' ...

Looking for assistance with accessing elements using the "document.getElementById" method in Javascript

Below is the code snippet I am working with: <html> <head> <script> function adjustSelection(option) { var selectList = document.getElementById("catProdAttributeItem"); if (option == 0) { ...

Tips for accurately determining the byte count of text within a TextArea

I am trying to figure out how to accurately calculate the byte size of text within a specific textarea. Although I have access to .Net libraries, I am looking for a Javascript solution instead. How many bytes does each character represent? What is the most ...

What can I do to prevent my panolens.js image from pausing its spin whenever a user clicks on it?

I've been working on setting up a 360 image background that rotates automatically and disabling most user interaction controls. However, I'm struggling with one issue – preventing any form of interaction from the user altogether. Whenever a use ...

Is there a way to invoke a C# method upon completion of the callback function in ScriptManager.RegisterStartupScript?

I am currently developing JavaScript methods that will be called from C# code. Once the JS methods are complete, I need to include C# code to send an email. Can anyone provide guidance on how to achieve this? ScriptManager.RegisterStartupScript(this, G ...

Slot context remains unclaimed

Context not available... Vue.component("custom-table", { name: 'CustomTable', template: "#custom-table", created: function() { console.log('Created', this.rows); }, mounted: function() { console.log(&a ...

VS code showing live server as installed but failing to function properly

After installing the live server extension, I noticed that my browser is not updating when I save my HTML or other files. What could be causing this issue? ...

Is there a way to get this reducer function to work within a TypeScript class?

For the first advent of code challenge this year, I decided to experiment with reducers. The following code worked perfectly: export default class CalorieCounter { public static calculateMaxInventoryValue(elfInventories: number[][]): number { const s ...

IE9 causing issues with Angularjs ng-route - views fail to display

I am new to AngularJS and currently working on developing an application using AngularJS along with Coldfusion for database data retrieval. However, I am facing compatibility issues specifically with IE9 (which is the default browser in my office). The ap ...

Using Javascript, Google Charts is able to interpret JSON data

I am currently working on integrating Google Charts and populating it with data from an external JSON file that I have generated using PHP's json_encode() function. After some effort, I managed to get Google Charts to display data successfully, but f ...

Guide to setting up an express route to utilize passport for secure authentication

I've recently made some adjustments to a boilerplate I created in es6 by downgrading it to an older version, es5. During this process, I had to modify the way I handle exports and requires instead of using imports, but now the routing is working smoot ...