## I have a problem with a subtraction of a Quantity using an Object X from Array 1 and Object Y of Array 2

php compare two array keys and values

array filter

php compare two arrays for matches

array_diff_assoc in php

php array diff keys

operations and algebraic thinking grade 4

array methods javascript

The function I use its OK but, when I try to do the subtraction both arrays gets subtracted

Variables:

let data1 = [ { ProductTotalId: 30, ProductId: 30, Quantity: 50 }, { ProductTotalId: 31, ProductId: 29, Quantity: 20 }, { ProductTotalId: 32, ProductId: 28, Quantity: 30 }, { ProductTotalId: 33, ProductId: 27, Quantity: 30 }, ]; let Remove = [ { ProductTotalId: 30, ProductId: 30, Quantity: 2 }, { ProductTotalId: 30, ProductId: 30, Quantity: 10 }, { ProductTotalId: 31, ProductId: 29, Quantity: 3 }, { ProductTotalId: 32, ProductId: 28, Quantity: 12 }, { ProductTotalId: 32, ProductId: 28, Quantity: 2 }, { ProductTotalId: 33, ProductId: 27, Quantity: 11 }, { ProductTotalId: 33, ProductId: 27, Quantity: 5 }, ] let data2

function:

data2 = data1; for (let value of data2) { for (let data of Remove) { if (value.ProductId === data.ProductId) { value.Quantity = value.Quantity - data.Quantity; } } } console.log(data1); console.log(data2);

The error is that data1 = data2, with Quantity Subtracted, what I need is that data2 gets subtracted and data 1 shouldn't suffer any changes.

link in Stackblitz: https://stackblitz.com/edit/typescript-u8mzii

Thanks for your time.

`data2 = data1`

is just creating another reference for object. You are not making copy of the object `data1`

here. So if you making changes to reference `data2`

it will affect the object refered by `data1`

(here same object).

to create copy of object use following

data2 = JSON.parse(JSON.stringify(data1));

**Array methods,** The arrays are objects, so we can try to use delete : The element was removed, but the array still has 3 elements, we can see let arr = [ 1 , 2 , 5 ] ; // from index - 1 (one step from the end) // delete 0 The calculation table is the same as above , minus the first row. alert(filterRangeInPlace(y,1,4)); // 3,1. 4 I have a problem with a subtraction of a Quantity using an Object X from Array 1 and Object Y of Array 2 Dec 18 '19. 4 Apply CSS to specific div on button click Jan 11.

Your problem is data structure.

`data2 = data1;`

This piece of code does not create 2 arrays.

Data2 is also pointing on that was initially pointed by Data1. In order to have 2 arrays, you must create an array and insert all the content from array1.

You should instead do this:

const data2= Object.assign([], data1);

Now you will have a clone.

**array_diff - Manual,** Compares array1 against one or more other arrays and returns the values in array1 that array elements by their value, without having to unset it by key, through a Basically: 2 x sort() is slower than 1 x array_diff() is slower than 1 x sort(). The same problem you will notice when checking with objects inside your array. Solve Equations Using the Addition Property of Equality. In all the equations we have solved so far, a number was added to the variable on one side of the equation. We used subtraction to “undo” the addition in order to isolate the variable. But suppose we have an equation with a number subtracted from the variable, such as [latex]x - 5=8

If you don't want `data1`

to suffer from changes in `data2`

, you have to create a copy of `data1`

as `data2`

. You can use following method to copy `data1`

into `data2`

.

let data2 = JSON.parse(JSON.stringify(data1))

This would first stringify(JSON it) `data1`

, then parse its result into `data2`

so it would be identical to `data1`

but not the same.

**Operations & Algebraic Thinking,** A.1. Represent addition and subtraction with objects, fingers, mental images, CCSS.Math.Content.K.OA.A.2. Solve addition and subtraction word problems, and arrays, and measurement quantities, e.g., by using drawings and equations having whole-number answers using the four operations, including problems in� Worksheets: Subtracting using objects or pictures (0-10) Below are six versions of our grade 1 math worksheet on subtracting using pictures / objects. Subtraction by "crossing out" objects helps students conceptualize subtraction and is a natural step from counting backwards to the writing of subtraction sentences.

**[XLS] CCSS Math Standards,** OA.1 Represent and solve problems involving addition and subtraction. Use addition to find the total number of objects arranged in rectangular arrays with up Example: If you have 2 dimes and 3 pennies, how many cents do you have? arrays, and measurement quantities, e.g., by using drawings and equations with a� Worksheets > Math > Grade 1 > Subtraction > Subtract using Pictures (0-20) Worksheets: Subtracting using objects or pictures (0-20) Below are six versions of our grade 1 math worksheet on subtracting using pictures / objects. Students are shown a number of objects with several of them "crossed out" and asked to write and solve the corresponding

**How To Do Math in JavaScript with Operators,** Assign values to x and y let x = 10; let y = 20; // Subtract x from y and assign We know that 1 + 1 should equal 2 , but this equation will have unexpected results. A common reason to use addition or subtraction in JavaScript would be Next in series: Understanding Arrays in JavaScript Report an issue. Count of subsets with sum equal to X using Recursion; Real-time application of Data Structures; Split the given string into Primes : Digit DP; Number of pairs in an array with the sum greater than 0; Count of subarrays of an Array having all unique digits; Minimum salary hike for each employee such that no employee feels unfair

**Numerical & Scientific Computing with Python: Matrix Arithmetics in ,** Matrix addition; Matrix subtraction; Matrix multiplication; Scalar product; Cross x = np.array([1,5,2]) >>> y = np.array([7,4,1]) >>> x + y array([8, 9, 3]) >>> x * y The matrix objects are a subclass of the numpy arrays (ndarray). If we want to perform matrix multiplication with two numpy arrays (ndarray), we have to use the � Or you could have a two-dimensional list of three courses, each containing two things you eat: (lettuce, tomatoes) and (steak, mashed potatoes) and (cake, ice cream) In the case of an array, our old-fashioned one-dimensional array looks like this: int [] myArray = {0,1,2,3}; And a two-dimensional array looks like this:

##### Comments

- I would suggest
`data2 = data1.map(r=>{ return {...r}})`

- @Eldar that wouldn't work: you would have two arrays, but holding the same objects.
- @Eldar As per my understanding this will work only for arrays.
`JSON.parse(JSON.stringify(obj);`

is more generic and can clone almost every object. (of course without methods) - @JBNizet is right my first snippet was wrong. And for the
`JSON.parse`

@Plochie i think there are reasons behind that some libraries exposes methods named`deepClone`

- @Eldar can u please elaborate. I know in this way you can only clone the state of object and not everything in it. But as far the data cloning goes, do u think this creates problem and in what scenarios?