数组循环中的Javascript对象赋值不正确

时间:2016-10-03 20:54:07

标签: javascript

我正在尝试将派生架构分配给对象。我想你会得到我想做的事情。这应该不是那么困难。我做错了什么?

let defaultValues = { 'apid': 1234, 'app': 'test' };
let hostFields = [
    {
        "name": "FirstName",
        "required": true,
        "label": {
            "singular": "First Name"
        }
    },
    {
        "name": "LastName",
        "required": false,
        "label": {
            "singular": "Last Name"
        }
    }
];
let appFields = ['FirstName', 'LastName', 'Bogus'];
let saveAll = [];

for (let i=0; i<hostFields.length; i++) {
    let f = hostFields[i];
    let save = defaultValues;

    save.requiredByHost = 0;
    save.requiredByApp = 0;

    if (f.required === true) {
        save.requiredByHost = 1;
    }

    if (appFields.indexOf(f.name) !== -1) {
        save.requiredByApp = 1;
    }

    if (save.requiredByHost === 1 || save.requiredByApp === 1) {
        save.schema = f;
        saveAll.push(save);
    }
});

console.log(saveAll);

0 个答案:

没有答案