Guys can you help with this customization of objects. I want to index the object properly but the current output of the object is that the index is the id of the store.
Sample Object
var stocks = [
{
"size": "2",
"count": 5,
"store": {
"id": 3,
"title": "Belconnen"
}
},
{
"size": "3",
"count": 4,
"store": {
"id": 4,
"title": "Canberra"
}
},
{
"size": "4",
"count": 4,
"store": {
"id": 5,
"title": "Bankstown"
}
},
{
"size": "5",
"count": 5,
"store": {
"id": 4,
"title": "Canberra"
}
}];
Current Output
{
0: {
"sizes": {
2: 3,
3: 0,
4: 0,
5: 0
}
}
},
{
1: {
"sizes": {
2: 0,
3: 3,
4: 0,
5: 0
}
}
},
{
2: {
"sizes": {
2: 0,
3: 0,
4: 3,
5: 0
}
}
},
{
3: {
"sizes": {
2: 0,
3: 0,
4: 0,
5: 3
}
}
}
My Current Code
var result = function (data) {
var stores = {},
sizes = Object.create(null),
i = 0;
data.forEach(function (item) {
var index = i;
if (!stores[index]) {
stores[index] = { sizes: {} };
Object.keys(sizes).forEach(function (key) {
stores[index].sizes[key] = 0;
});
}
if (!sizes[item.size]) {
Object.keys(stores).forEach(function (key) {
stores[key].sizes[item.size] = 0;
sizes[item.size] = true;
});
}
stores[index].sizes[item.size] = item.count;
i++;
});
return stores;
}(stocks);
console.log(result);
My Expected Output
{
0: {
"sizes": {
2: 3,
3: 0,
4: 0,
5: 0
}
}
},
{
1: {
"sizes": {
2: 0,
3: 3,
4: 0,
5: 3
}
}
},
{
2: {
"sizes": {
2: 0,
3: 0,
4: 3,
5: 0
}
}
}
Thanks in advance.