简体   繁体   English

从JS对象将属性值重新分配给另一个

[英]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. 我正在尝试从JSON A到JSON B分配商品价格,设法获取价格并将其重新分配给属性,而不是整个对象。

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? 这是我的代码段,它从第一个对象获取价格并将其重新分配给TotalOrignialValue但是如何将其推回到newJson对象?

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. 看起来您从JSON A到JSON B的唯一连接是项目上的代码数组。

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 您可以遍历JSON a中的条目,通过检查代码值在JSON B中找到相应的项目,然后直接在JSON B条目上分配值

 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. 这也将减少数组的使用和JSON项之间的循环。 On a list of 2 its not so bad, but add a few hundred/thousand and it will become noticeable. 在2的列表上,它还不错,但是加了几百/千,它将变得很明显。

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. 使用Lodash使您的生活变得更加轻松,使用lodash可以满足您的需求,而使用lodash可能更简洁。

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/ 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. 遍历OrderLines键值(它是一个数组),然后将每个TotalOriginalValue值替换为items.TransactionLine数组中的响应值。

 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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值重新分配给对象属性时出错 - Error reassigning a value to an object property TS/JS - 如果数组中对象的属性与单独对象中的另一个属性匹配,则从对象数组中获取“值” - TS/JS - Get "value" from an Array of Objects if a Property of an Object from the Array matches another Property from a separate Object 根据另一个属性的值更新嵌套的 object 属性 - JS - Update nested object property based on the value of another property - JS 为什么给一个 object 赋值给另一个 object,然后重新赋值原来的 object 会改变这两个对象? - Why does assigning an object a value to that of another object, and then reassigning the original object change both objects? 从 props 值重新分配状态 - Reassigning state from props value 从当前对象获取属性以在另一个对象中使用属性值 - Getting a property from current object to use the property value in another 将对象的值匹配到js中的另一个数组 - match value from object to another array in js 根据另一个属性的值将属性从一个对象传输到另一个对象 - Transferring Properties from one to another object based on the value of another property Javascript - 使用来自相同 object 的另一个值更新 object 属性 - Javascript - updating an object property with another value from the same object Ramda Js:使用同一对象中的值设置对象的属性 - Ramda Js: Setting property on an object using a value from the same object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM