I have an array (json) of products (around 5000) where each product is represented in two arrays as shown below;
["PC01","Product 1","11"],
["PC01","Product 1","17"]
The only difference is, the 11 and 17 strings both representing the first and second price of the product.
I am trying to find a way to merge this array into a single one so that each product is represented as one such as: ["PC01", "Product 1", "11", "17"]
I have tried looping and forming an object of products which I found not quite safe due to various reasons and one of them being the order of the prices are unknown. So at first I might get the highest price first and lowest price second while on the other hand, another array would have the lowest one first and highest the second.. This is my current code;
var products = [];
$.getJSON('data.json', function (data) {
$.each(data, function (key, val) {
if (!key == 0) {
// because I use the previous arrays info,
// only start if we HAVE a previous array hence;
if (data[key][0] != data[key - 1][0]) {
var o = {}
o.ProductCode = val[0];
o.ProductName = val[1];
o.Price1 = val[3];
o.Price2 = data[key - 1][3];
products.push(o);
}
}
});
});
As I said, this relies on the fact that each product exists as two arrays and assumes the second price is the highest.
So, I am basically trying to find the most optimal way to parse the json to merge all double arrays and would be grateful if you could give me some ideas..
EDIT: Sample Data:
Note: Sample data have more information than I initially wrote in my question such as stock quantity, VAT rate etc. but omitted by me to prevent any confusions.
[
[
"AD.0001 ",
"AD ALOE VERA MOUTHWASH w�th FLUORIDE 250ML ",
" 691",
" 11.51",
" 16",
" 96"
],
[
"AD.0001 ",
"AD ALOE VERA MOUTHWASH w�th FLUORIDE 250ML ",
" 691",
" 17",
" 16",
" 96"
],
[
"AD.0002 ",
"AD ALOE VERA MOUTHWASH 250ML ",
" 692",
" 11.51",
" 16",
" 75"
],
[
"AD.0002 ",
"AD ALOE VERA MOUTHWASH 250ML ",
" 692",
" 17",
" 16",
" 75"
]
]