Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript subtracting values in arrays

Hi all, please note I am very much a beginner lol. And I have run into a bit of a snag.

var stockBlock = ['abc', 123, 500, 500, 2000];
var jobBlock = ['def', 123, 100, 100, 300];

I have these two arrays in my javascript. I want to subtract one value of jobBlock (index 4 - 300) from index 4 of stockBlock (2000) and create a new array as follows:

var residueBlock = [ghi, 123, 500, 500, 1700];

But I have no idea how. I have tried using .splice, but all that does is display the removed element (2000). I really am very lost. Any guidance would be greatly appreciated.

Thank You
 
Solution
Maybe you could try:
Code:
var residueBlock = ['ghi', stockBlock[1], stockBlock[2], stockBlock[3], stockBlock[4] - jobBlock[4]]
You can also try:
Code:
var residueBlock = [...stockBlock]; // this clones the stockBlock array
residueBlock[0] = "ghi"; // updates the zeroth element of residueBlock
residueBlock[4] = residueBlock[4] - jobBlock[4]; // updates the fourth element of residueBlock
 
Hi all, please note I am very much a beginner lol. And I have run into a bit of a snag.

var stockBlock = ['abc', 123, 500, 500, 2000];
var jobBlock = ['def', 123, 100, 100, 300];

I have these two arrays in my javascript. I want to subtract one value of jobBlock (index 4 - 300) from index 4 of stockBlock (2000) and create a new array as follows:

var residueBlock = [ghi, 123, 500, 500, 1700];

But I have no idea how. I have tried using .splice, but all that does is display the removed element (2000). I really am very lost. Any guidance would be greatly appreciated.

Thank You
Hi there,

Welcome to Code Forum!

I see @ivan.moony was able to help you. Make sure to mark his post as a solution if it solved your issue if not and you found it elsewhere then please post it here to help others.

And, for future reference, please use our BBCode feature before posting any code. It makes it easier to read and copy when you do use this feature. You can view more information about our BBCode here.
 
Thank you, I will look into BBCode.

Instead of making another post, can I add a question here?

I have two csv files I have read with javascript and converted them into arrays. I want to compare values between them eg if the value of index[3] row 1 of the first array is bigger/smaller than index[3] row 1 of the second array, then.......etc etc. But how do you specify which array? They are not named? Do I need to merge them?

Appreciate the help 🙂
 

New Threads

Buy us a coffee!

Back
Top Bottom