简体   繁体   中英

Reassigning from JS Object A property value to another

I'm trying to assign prices to my items from a JSON A to JSON B, managed to get the prices and reassign it to the property but not to the whole object.

here's a snippet of my code, which gets the prices from the first Object and reassigning it to TotalOrignialValue however how can I push it back to the newJson object?

Is there a more pleasing way of achieving this?

 // Code goes here var items = { "TransactionLine": [ { "Product": { "Id": null, "Codes": [ "1112" ], "Sku": null }, "TotalValue": 2.35, }, { "Product": { "Id": null, "Codes": [ "1113" ], "Sku": null }, "TotalValue": 2.15, } ], "CustomData": {} }; var itemPrice = []; for (var i = 0; i < items.TransactionLine.length; i++) { var el = items.TransactionLine[i]; itemPrice.push(el.TotalValue); console.log(el.TotalValue); } var newJson = { "OrderLines": [ { "Product": { "Id": 9, "Codes": [ "1113" ], "Sku": "CS1113" }, "TotalOriginalValue": 0, // asign the price here }, { "Product": { "Id": 21, "Codes": [ "1112" ], "Sku": "CS1112" }, "TotalOriginalValue": 0, // asign the price here } ] }; var newPrice = []; for (var x = 0; x < newJson.OrderLines.length; x++) { var xd = newJson.OrderLines[x].TotalOriginalValue; xd = itemPrice[x]; newjson = { "TotalOriginalValue": xd }; newPrice.push(newjson); } console.log('newJSON >> ', newPrice); 

it looks like your only connection from JSON A to JSON B is the codes array on the items.

You could loop over entries in JSON a, find the corresponding item in JSON B by checking the codes values, and assign the values directly on JSON B entries

 var items = { "TransactionLine": [ { "Product": { "Id": null, "Codes": [ "1112" ], "Sku": null }, "TotalValue": 2.35, }, { "Product": { "Id": null, "Codes": [ "1113" ], "Sku": null }, "TotalValue": 2.15, } ], "CustomData": {} }; var newJson = { "OrderLines": [ { "Product": { "Id": 9, "Codes": [ "1113" ], "Sku": "CS1113" }, "TotalOriginalValue": 0, // asign the price here }, { "Product": { "Id": 21, "Codes": [ "1112" ], "Sku": "CS1112" }, "TotalOriginalValue": 0, // asign the price here } ] }; items.TransactionLine.forEach(item=>{ var match = newJson.OrderLines.find(entry=>entry.Product.Codes[0] === item.Product.Codes[0]); if (!match) { return; } match.TotalOriginalValue = item.TotalValue; }); console.log(newJson); 

This will also cut out the use of the array and a loop through the items JSON. On a list of 2 its not so bad, but add a few hundred/thousand and it will become noticeable.

Using Lodash makes your life so much easier that does what you need using lodash there is probably an even more succinct way of doing it with it.

var items = {
    "TransactionLine": [
      {
        "Product": {
          "Id": null,
          "Codes": [
            "1112"
          ],
          "Sku": null
        },
        "TotalValue": 2.35,
      },
      {
        "Product": {
          "Id": null,
          "Codes": [
            "1113"
          ],
          "Sku": null
        },
        "TotalValue": 2.15,
      }
    ],
    "CustomData": {}
  };



  var newJson = {
    "OrderLines": [
          {
            "Product": {
              "Id": 9,
              "Codes": [
                "1113"
              ],
              "Sku": "CS1113"
            },
            "TotalOriginalValue": 0, // asign the price here
          },
          {
            "Product": {
              "Id": 21,
              "Codes": [
                "1112"
              ],
              "Sku": "CS1112"
            },
            "TotalOriginalValue": 0, // asign the price here
          }
        ]
  };

var test = _.map(items.TransactionLine, (item,index) => {
return _.set(newJson.OrderLines[index], 'TotalOriginalValue', item.TotalValue)
})

console.log(test)

https://jsfiddle.net/k6vdyhx7/124/

Iterate over OrderLines key value, which is an array, then replace every TotalOriginalValue value with responding value from the items.TransactionLine array.

 var items = {TransactionLine:[{Product:{Id:null,Codes:["1112"],Sku:null},TotalValue:2.35},{Product:{Id:null,Codes:["1113"],Sku:null},TotalValue:2.15}],CustomData:{}}, newJson = {OrderLines:[{Product:{Id:9,Codes:["1113"],Sku:"CS1113"},TotalOriginalValue:0},{Product:{Id:21,Codes:["1112"],Sku:"CS1112"},TotalOriginalValue:0}]}; newJson.OrderLines.forEach((v,i) => v.TotalOriginalValue = items.TransactionLine[i].TotalValue); console.log(newJson); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM