How can I add this?

I have an array [ [ 'QUARTER', 0.25 ], [ 'QUARTER', 0.25 ] , ['PENNY' , 0.01] ]

How can I change this to [ [ 'QUARTER', 0.50 ], ['PENNY' , 0.01] ]

I feel like this is a lot more simple than I’m making it out to be. I’m not looking for a literal answer, just point me in the right direction. Thanks in advance.

You have a mismatched number of brackets, so the specifics will differ depending on what your array actually looks like.

You may find it easier to store data in an object and then create an array after you are done, but if you just want to turn one array into the other, you could manually iterate over it and compose a new array that adds the numeric value if the string value is already present (pushing the 2 element sub-array otherwise). You could also use an array.map() function.

I went ahead and fixed the number of brackets. Thanks for pointing that out. I’ll try out your suggestions.

Call your array for example A

A = [ [ ‘QUARTER’, 0.25 ], [ ‘QUARTER’, 0.25 ] , [‘PENNY’ , 0.01] ]

if you want to change it to:

[ [ ‘QUARTER’, 0.50 ], [‘PENNY’ , 0.01] ]

If I really understand your question you have to check the followíng steps:

  1. delete the first (or the second (they are equal)) element of A follows:
    A.shift(A[0])

After that:

  1. replace the 0.25 with the 0.5 as follows:
    A[0][1] = 0.5