[英]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.