Removing elements from an array in JavaScript using subtraction

I have 2 arrays structured like this :

VM.dataTotalList = result.map(item => {
                    return {
                      idEquipment : item['0'],
                      timestamp   : item['1'],
                      value       : item['2']
                    };
                });
VM.dataFreeList = result.map(item => {
                    return {
                      idEquipment : item['0'],
                      timestamp   : item['1'],
                      value       : item['2']
                    };
                });

I am interested in extracting only the 'value' property from both arrays. I would like to end up with an array of the same format as a result. Could someone please guide me on how to achieve this?

Thank you!

UPDATE1

        VM.dataFreeList = [];
        VM.dataTotalList = [];
        /**
         * Retrieving the total swap value of an idequipment
         * */
        SwapDataService.getSwapTotalDataList().then((result) => {
            // renaming properties in the data array
            VM.dataTotalList = result.map(item => {
                return {
                  idEquipment : item['0'],
                  timestamp   : item['1'],
                  value       : parseInt(item['2'])
                };
            });
        }).then(() => {

        });

        /**
         * Retrieving the free swap value of an idequipment
         * */
        SwapDataService.getSwapFreeDataList().then((result) => {
            // renaming properties in the data array
            VM.dataFreeList = result.map(item => {
                return {
                  idEquipment : item['0'],
                  timestamp   : item['1'],
                  value       : parseInt(item['2'])
                };
            });
            $log.info('total', VM.dataTotalList);
            $log.info('free', VM.dataFreeList);
            VM.newdataList = VM.dataTotalList.map((item, index) => {
                item['value'] -= VM.dataFreeList[index]['value'];
                return item;
            });


            $log.info('new array', VM.newdataList);
        });

Here's a glimpse of the data:

total 
(18) […]
​
0: Object { idEquipment: "b827eb008bb1", timestamp: 1597948232825, value: 256, … }
​
1: Object { idEquipment: "b827ebb4ceff", timestamp: 1597948294797, value: 0, … }

free 
(17) […]
​
0: Object { idEquipment: "b827eb7945bd", timestamp: 1597948315924, value: 102140, … }
​
1: Object { idEquipment: "b827eb519c39", timestamp: 1597947610314, value: 102396, … }
​
2: Object { idEquipment: "b827eb28ab09", timestamp: 1597947933909, value: 100604, … }

And the resulting new array is:

new array 
(18) […]
​
0: Object { idEquipment: "b827ebb4ceff", timestamp: 1597948294797, value: 0, … }
​
1: Object { idEquipment: "b827eba1e021", timestamp: 1597948154016, value: 768, … }
​
2: Object { idEquipment: "b827eb15ff2c", timestamp: 1597947773103, value: 1792, … }

Answer №1

updatedList = totalDataList.map((element,pos) => {
    element['quantity'] -= freeDataList[pos]['quantity'];
    return element;
})

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

Struggling to get a price calculator up and running efficiently

The code I have should be functioning, however, when I insert it into the code module in the Divi theme on WordPress, it doesn't work. Interestingly, when I try it in a notepad, it works perfectly fine but fails to function on the website itself. () ...

Is there a way to incorporate a Gif into the background of a three.js project?

function initializeGame() { cubeSize = 200; fieldDepth = 50; fieldWidth = 200; fieldHeight = 200; initObstacles = _ => _; // set the game size var WIDTH = 1000; var HEIGHT = 500; // set camera properties var VIEW_ANGLE = 40, ...

The CSS classes for the dropzonejs are not being properly updated for editing purposes

I'm currently facing an issue where I am attempting to include an editable area inside a dropzone, but for some reason, the editable area is not visible within the dropzone and the CSS classes are not being applied. <a href="#" editable-text="us ...

Monitor the completion status of all Ajax requests using only JavaScript

I am aware of the ajaxStop method in jQuery. $(document).ajaxStop(function() { //Do something }); If jQuery is not available, is there a way to achieve this with pure JavaScript instead? If so, could you please provide an example? Thanks ...

Access cookie information within a Vue application by reading it within the router or component

There is a url (*/webapp/callback) in my vue.js app that gets redirected (http 302) from another application, bringing along some cookies. I'm trying to figure out how I can read these cookies when the component mounts and then store them in vuex stat ...

The inputs for Node express middleware are unclear and lack definition

I am currently exploring Node.js as a potential replacement for my existing DOT NET API. I have created middleware to enforce basic non-role authorization in my application, but I am encountering compilation problems with the function inputs. Compilation ...

Stop music with one click, code in Javascript

I am encountering an issue with a set of 5 div boxes on my website. Each box is supposed to play a specific audio track when clicked, based on data attributes. However, I'm having trouble pausing the previous track when clicking on a new box, resultin ...

Verify if the user is currently inputting text within a specific range of characters within

I am dealing with a specific issue involving a textarea. By default, this textarea contains a string of any type, but a user is only able to type inside opening and closing brackets. For example, "Leave[]" allows typing within the brackets but not outsid ...

Player-Oriented Online Game: Addressing Target Accuracy Challenges in ctx.setTransform

I'm currently developing a web game and my goal is to ensure that the player remains at the center of the screen. However, as the player moves further away from the center, the accuracy decreases. I've attempted using ctx.setTransform, which work ...

Positioning a material UI dialog in the middle of the screen, taking into account variations in its height

Dealing with an MUI Dialog that has a dynamic height can be frustrating, especially when it starts to "jump around" the screen as it adjusts to fit the content filtered by the user. Take a look at this issue: https://i.stack.imgur.com/IndlU.gif An easy f ...

When NextJS calls a dynamic page in production, it redirects to the root page

My Desired Outcome When a user inputs https://www.example.com/test, I want them to receive the content of the NextJS dynamic route /test/index.js. This functionality is successful in my local environment. The Current Issue Despite a user entering https:/ ...

Ways to pinpoint a particular division and switch its class on and off?

Consider this scenario, where a menu is presented: function toggleHiddenContent(tabClass) { let t = document.querySelectorAll(tabClass); for(var i = 0; i<t.length; i++) { t[i].classList.toggle="visible-class"; } } .hidden-conten ...

Is it possible to utilize the `.apply()` function on the emit method within EventEmitter?

Attempting to accomplish the following task... EventEmitter = require('events').EventEmitter events = new EventEmitter() events.emit.apply(null, ['eventname', 'arg1', 'arg2', 'arg3']) However, it is ...

What is the best way to store checkbox statuses in local storage and display them again in a JavaScript to-do list?

I'm currently working on a to-do list application using basic JavaScript. One issue I'm facing is saving the checked status of the checkbox input element and displaying it again after the page is refreshed. Since I'm still learning JavaScrip ...

The C# [WebMethod] will not trigger if the Content-Type "application/Json" is missing

After creating a C# WebMethod, I was able to successfully call it using Ajax, angular, and Postman when adding the header Content-Type: 'application/Json'. Here is an example of the HTTP request that worked: $http({ url: 'default.aspx/G ...

Swapping out the JSON data from the API with HTML content within the Vue.js application

I am currently working on a project involving Vite+Vue.js where I need to import data from a headless-cms Wordpress using REST API and JSON. The goal is to display the titles and content of the posts, including images when they appear. However, I have enco ...

Creating distinctive ng-form tags within a form using ng-repeat in Angular

I need help with creating a form that includes a table looping over a list of objects. Each object should have checkboxes for the user to check/uncheck attributes. The issue I am facing is setting the ng-model attribute on the checkboxes. This is what my ...

Implement a JavaScript function that toggles the visibility of a div based on changes to an anchor tag

My objective is to have the lds-roller div only displayed when the text within the anchor tag with the ID of searchFor is equal to _. This change in text should occur with an HTTP response. <div class="lds-roller"><div></div><div> ...

Error: Unable to access the 'map' property of an undefined object......Instructions on retrieving a single post

import React,{useEffect, useState} from 'react' //import {Link} from 'react-router-dom' import { FcLikePlaceholder, FcComments } from "react-icons/fc"; const SinglePost = () => { const [data,setdata] = useState([]) co ...

Sharing data between AngularJS 1.5.x components using a shared service

As a newcomer to angularjs, I have a few questions regarding a project I am working on. The task involves retrieving a complex tree-like form object from the server and binding it to 4 different components or tabs. To achieve this, I created a Service spec ...