0

这是我的测试对象:

let testObj = {
    addressOne: "delhi",
    addressTwo: "varansi",
    cityCode: "IN-BR-DH",
    countryCode: "IN",
    domainId: 75,
    emailId: "emailId3{{i}}@gmail.com",
    firstName: "automate",
    lastName: "string",
    mobileCode: "+91",
    mobileNumber: "{{userMobileNumber}}",
    orgId: 170,
    roleId: 4,
    stateCode: "IN-BR",
    userName: "automation98",
    zipCode: 656689
}

我想创建 15 个场景,其中该对象的每个值都将为“空”

此代码打印所有键和值:

for(let key of Object.keys(testObj) ){
    console.log(key, testObj[key])
}

但我想要一些能设置的东西

all keys and values in first iteration,
addressOne = null in the second iteration,
addressTwo = null in the third iteration,
cityCode  = null in fourth iteration,

等等..

请提出一种方法来做到这一点。

4

1 回答 1

1

在每次迭代中取一个键并将其设置为null. 重要的是创建对象的克隆。以下代码在每次迭代中创建一个对象。

for (let key of Object.keys(testObj)) {
    let clone = {
        ...testObj,
        [key]: null
    };
    console.log(clone);
}

此代码使用扩展运算符和计算属性。这些是新的 javascript 功能,您可能希望用旧功能替换。

于 2020-08-17T14:04:12.200 回答