gpt4 book ai didi

javascript - 在 Javascript 中使用变量拆分和连接

转载 作者:行者123 更新时间:2023-12-01 23:17:19 24 4
gpt4 key购买 nike

我有 ff。数组的 JSON 负载

  {
"Meta": {
"LastAvailableDateTime": "",
"FirstAvailableDateTime": "",
"TotalPages": 1,
"Count": 2
},
"Links": {
"Next": null,
"Previous": null,
"Self": ""
},
"Data": {
"Transactions": [
{
"AuthCode": "175601",
"CardId": "************8104"
},
{
"AuthCode": "783453",
"CardId": "************8104"
},
{

"AuthCode": "12132",
"CardId": "************8104"
}
]
}
}

我想通过将 CardId 重命名为 MaskedCardId 并添加一个名为 CardId 的新参数和实际的卡片 ID 来覆盖此有效负载。

我的代码是ff。下面

var json = //The Parsed JSON Payload;
var CardId = "123458104"

json = JSON.parse(
JSON.stringify(json).split('"CardId":')
.join('"CardId":CardId,"MaskedCardId":')
);

我已经可以用名为 MaskedCardId 的重命名参数替换 CardId。与 ff。代码

 json = JSON.parse(JSON.stringify(json).split('"CardId":').join('"MaskedCardId":'));

但是每当我尝试添加一个名为 CardId 的具有变量值的新参数时,它都会给出错误。

failed with error: Javascript runtime error: "SyntaxError: Unexpectedtoken: C

如何使这行代码符合我的预期目的?时间差

最佳答案

记住旧值

const data = {
"AuthCode": "1",
"PostingDate": "2021-05-06",
"Amount": "6000.28",
"CardId": "************8104",
};

const CardIdNew = "123458104";
const CardIdOld = data.CardId;

data.CardId = CardIdNew;
data.MaskedCardId = CardIdOld;

console.log(data);

或者更简单地说:

const data = {
"AuthCode": "1",
"PostingDate": "2021-05-06",
"Amount": "6000.28",
"CardId": "************8104",
};

data.MaskedCardId = data.CardId; // Add
data.CardId = "123458104"; // Override

console.log(data);

问题编辑后的附录

循环包含事务的数组:

const json = {"Meta":{"LastAvailableDateTime":"","FirstAvailableDateTime":"","TotalPages":1,"Count":2},"Links":{"Next":null,"Previous":null,"Self":""},"Data":{"Transactions":[{"AuthCode":"175601","CardId":"************8104"},{"AuthCode":"783453","CardId":"************8104"},{"AuthCode":"12132","CardId":"************8104"}]}};

const CardId = "123458104"

json.Data.Transactions.forEach(ob => {
ob.MaskedCardId = ob.CardId;
ob.CardId = CardId;
});

console.log(json);

关于javascript - 在 Javascript 中使用变量拆分和连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68653604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com