#c# #typescript
#c# #typescript
Вопрос:
У меня есть два объекта JSON (напечатаны здесь с использованием JSON.stringify (массив объектов)) Данные GPRows являются
[
{
"shopName":"Testing One",
"state":"NSW",
"yearMonth":"20203",
"id":29,
"shopId":1,
"paintSale":80000,
"panelSale":80000,
"partsSale":80000,
"otherSale":80000,
"paintCost":80000,
"panelCost":80000,
"partsCost":80000,
"otherCost":80000,
"panelWages":80000,
"paintWages":80000,
"depreciation":80000,
"forecastedSales":80000,
"expenses":80001
},
{
"shopName":"Not yours",
"state":"SA",
"yearMonth":"20204",
"id":28,
"shopId":2,
"paintSale":85000,
"panelSale":80000,
"partsSale":80000,
"otherSale":80000,
"paintCost":80000,
"panelCost":80000,
"partsCost":80000,
"otherCost":80000,
"panelWages":80000,
"paintWages":80000,
"depreciation":80000,
"forecastedSales":80000,
"expenses":80000
},
{
"shopName":"Testing One",
"state":"NSW",
"yearMonth":"20201",
"id":31,
"shopId":1,
"paintSale":75000,
"panelSale":75000,
"partsSale":75000,
"otherSale":75000,
"paintCost":60000,
"panelCost":42000,
"partsCost":45000,
"otherCost":20000,
"panelWages":75000,
"paintWages":75000,
"depreciation":75000,
"forecastedSales":75000,
"expenses":75000
}
]
и
Данные BudgetTargets являются
[
{
"shopName":"Testing One",
"state":"NSW",
"yearMonth":"20202",
"shopId":1,
"sales":487500,
"costs":80000,
"expenses":90000,
"netprofit":25000,
"arc":2100,
"numVehicles":232,
"ppv":108,
"wagesperc":10,
"gPperc":40
},
{
"shopName":"Not yours",
"state":"SA",
"yearMonth":"20204",
"shopId":2,
"sales":487500,
"costs":80000,
"expenses":90000,
"netprofit":25000,
"arc":2100,
"numVehicles":232,
"ppv":108,
"wagesperc":10,
"gPperc":40
}
]
Я либо хочу сопоставить их вместе в YearMonth и shopId? или, поскольку они имеют одинаковые имена значений (которые я не хочу перезаписывать), возможно, функцию, чтобы я мог запрашивать значение BudgetTargets для этой конкретной записи в моем angular, когда я * ngFor перебираю массив GPRows
Ответ №1:
Сопоставьте список GPRows и проверьте, есть ли элемент, соответствующий условию в списке BudgetTargets, затем объедините их в объекте, иначе просто вернется элемент GPRows
const GPRows = [ { "shopName": "Testing One", "state": "NSW", "yearMonth": "20203", "id": 29, "shopId": 1, "paintSale": 80000, "panelSale": 80000, "partsSale": 80000, "otherSale": 80000, "paintCost": 80000, "panelCost": 80000, "partsCost": 80000, "otherCost": 80000, "panelWages": 80000, "paintWages": 80000, "depreciation": 80000, "forecastedSales": 80000, "expenses": 80001 }, { "shopName": "Not yours", "state": "SA", "yearMonth": "20204", "id": 28, "shopId": 2, "paintSale": 85000, "panelSale": 80000, "partsSale": 80000, "otherSale": 80000, "paintCost": 80000, "panelCost": 80000, "partsCost": 80000, "otherCost": 80000, "panelWages": 80000, "paintWages": 80000, "depreciation": 80000, "forecastedSales": 80000, "expenses": 80000 }, { "shopName": "Testing One", "state": "NSW", "yearMonth": "20201", "id": 31, "shopId": 1, "paintSale": 75000, "panelSale": 75000, "partsSale": 75000, "otherSale": 75000, "paintCost": 60000, "panelCost": 42000, "partsCost": 45000, "otherCost": 20000, "panelWages": 75000, "paintWages": 75000, "depreciation": 75000, "forecastedSales": 75000, "expenses": 75000 } ];
const BudgetTargets = [ { "shopName": "Testing One", "state": "NSW", "yearMonth": "20203", "shopId": 1, "sales": 487500, "costs": 80000, "expenses": 90000, "netprofit": 25000, "arc": 2100, "numVehicles": 232, "ppv": 108, "wagesperc": 10, "gPperc": 40 }, { "shopName": "Not yours", "state": "SA", "yearMonth": "20204", "shopId": 2, "sales": 487500, "costs": 80000, "expenses": 90000, "netprofit": 25000, "arc": 2100, "numVehicles": 232, "ppv": 108, "wagesperc": 10, "gPperc": 40 } ];
const dataMerged = GPRows.map(GPItem => {
const budgetTargetItem = BudgetTargets.find(bTItem =>
bTItem.yearMonth == GPItem.yearMonth amp;amp; bTItem.shopId ==
GPItem.shopId);
return budgetTargetItem !== undefined ? {
...GPItem,
...budgetTargetItem
} : GPItem
});
document.querySelector("#app").innerHTML = "<pre>" JSON.stringify(dataMerged,null, 2) "</pre>";
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
text-align: center;
}
<div id="app"></div>
Комментарии:
1. это мой результат. не похоже на то, что я хотел? pastebin.com/rgN6WN0y
2. Я уверен, что вы что-то забыли, я просто отредактирую ответ, добавив фрагмент кода для лучшего понимания, я надеюсь, это поможет вам