0

I've got two JSON objects (printed here using JSON.stringify(array of objects)) GPRows data is

[
   {
      "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
   }
]

and

BudgetTargets data is

[
   {
      "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
   }
]

I either want to map them together on yearMonth and shopId? or since they have the same value names (which i don't want to overwrite) maybe a function so that I can query BudgetTargets value for this specific entry in my angular when I *ngFor loop the GPRows array

0

1 Answer 1

1

Map the GPRows list and check if there is an element conforming to the condition on the BudgetTargets list, then merging the two on a object ,else just returns the GPRows item

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 && 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>

Sign up to request clarification or add additional context in comments.

2 Comments

this is my result. doesn't look right to what I wanted? pastebin.com/rgN6WN0y
I'm sur you forgot something , i juste edit the answer by add code snippet for better understand , I hope it will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.