拆分并加入 Javascript 中的变量

Split and Join with Variable in Javascript

我有 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: Unexpected token: C

如何使这行代码符合我的预期目的? TIA

记住旧值

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

拆分和合并是针对字符串的;将 JSON 作为一个对象来操作。

json.MaskedCardId = json.CardId;
json.CardId = CardId;

我想这应该可行。联接必须像这样 .join('"CardId":"' + CardId + '", "MaskedCardId":') 因为您要将字符串变量添加到对象中,所以不要忘记将 "' + CardId + '":

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

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

在这里试试

var json = {
  someId: 10,
  somekey: "somevalue",
  cards: [
     {
      CardId: "*********67",
      otherkey: "othervalue"
     },
     {
      CardId: "*********67",
      otherkey: "othervalue"
     }
  ]
}
var CardId = "123458104"

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

console.log(json);