Welcome fellow programmers and like-minded individuals,
Every time a user clicks on a basket to add a product, an object named field_x is created, where x increments by 1 starting from 1.
My objective is to develop a function that can identify duplicate items, merge their piece values, update the first item with this new total, and remove any other matching arrays.
Here is an example of what I aim to achieve in string format:
Input:
field_1 [{"name":"A","piece":1,"active":"true"}]
field_2 [{"name":"B","piece":1,"active":"true"}]
field_3 [{"name":"A","piece":1,"active":"true"}]
field_4 [{"name":"C","piece":1,"active":"true"}]
field_5 [{"name":"A","piece":1,"active":"true"}]
field_6 [{"name":"A","piece":1,"active":"true"}]
field_7 [{"name":"B","piece":1,"active":"true"}]
field_8 [{"name":"C","piece":1,"active":"true"}]
field_9 [{"name":"A","piece":1,"active":"true"}]
field_10 [{"name":"A","piece":1,"active":"true"}]
Output:
field_1 [{"name":"A","piece":6,"active":"true"}]
field_2 [{"name":"B","piece":2,"active":"true"}]
field_3 [{"name":"C","piece":2,"active":"true"}]
I want to emphasize that I do not plan to use eval in the final version as it is not considered best practice. I have alternative methods to replace it.
The current code I have does not offer me a solution, and I am unsure about the next steps to take.
<script src="js/underscore.js"></script>
<script>
var fieldsNumber = 10;
var field_1 = new Array();
var field_2 = new Array();
var field_3 = new Array();
var field_4 = new Array();
var field_5 = new Array();
var field_6 = new Array();
var field_7 = new Array();
var field_8 = new Array();
var field_9 = new Array();
var field_10 = new Array();
// Rest of the JavaScript code...
Any suggestions or feedback are welcomed!