У меня есть JSON, который выглядит следующим образом (копируется из Dev инструментов инспектора)Упорядочивание объекта в JavaScript с помощью подчеркивания
salePurchases
salesPeriods: Array[2]
0: Object
data: Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
period: "2011"
1: Object
data: Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
period: "2012"
purchasePeriods: Array[2]
0: Object
data: Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
period: "2011"
2: Object
data: Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
period: "2012"
Я хочу, чтобы вытащить его на части и сделать его создающим новый объект, который выглядит следующим образом -
OrderedByYears:
2011:
data: Array[10]
0: Object // this should be the 1st item from the salesPeriods array with a period of 2011
1: Object // this should be the 1st item from the purchasePeriods array with a period of 2011
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object // this should be the 5th item from the salesPeriods array with a period of 2011
10: Object // this should be the 5th item from the purchasePeriods array with a period of 2011
2012:
data: Array[10]
0: Object // this should be the 1st item from the salesPeriods array with a period of 2012
1: Object // this should be the 1st item from the purchasePeriods array with a period of 2012
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object // this should be the 5th item from the salesPeriods array with a period of 2012
10: Object // this should be the 5th item from the purchasePeriods array with a period of 2012
Я основываю устройство на ключевом «периоде», а затем чередуюся между объединением каждого массива, но чередуя между продаваемыми объектами продаж и периодами покупки.
Я уверен, что это относительно просто с использованием подчеркивания. Кто-нибудь знает, если это легко сделать? Объяснение предпринятых шагов было бы высоко оценено.
Предполагая, что вы застряли, и это не просто вопрос «напишите этот код для меня» - где вы застряли? –
Я застрял на подходе, который я должен взять, я сейчас попробую ответить на ответ Джона. – Finnnn