如何在JavaScript中将值从一个数组添加到另一个数组

时间:2018-11-28 10:28:05

标签: javascript vue.js ecmascript-6 vuejs2

  

我正在尝试将以下数组中的值添加到另一个数组中

[
    {"criteriaName": "CRITERIA 1"},
    {"criteriaType": "Dependent"},
    {"table": "Table1"},
    {"column": "Column1"},
    {"joinType": "OR"},
    {"operator": ">="},
    {"valueType": "SQL"},
    {"dataType": "NUMBER"},
    {"format": "Config"},
    {"parameterMandatory": "YES"},
    {"link": "KB"},
    {"sequence": "SEQ1"},
    {"value": "VAL"},
    {"description": "abcde"}
]
  

从上面的数组中,我试图将每个值添加到下面的嵌套对象中。

     

需要将上述数组中的每个值添加到以下数组中的每个模型   下面的一个是目标数组。

     

我试图将上面的数组值添加到下面

formFields = [
    {
        "title": "Criteria Details",
        "columns": 2,
        "fields": {
            "criteriaName": {
                "type": "text",
                "label": "Criteria Name",
                "id": 'criteriaName',
                "model": "",
                "required": true,
                "show": true,
                "rules": [
                    v => !!v || 'Criteria Name is required',
                ]
            },
            "criteriaType": {
                "type": "select",
                "label": "Criteria type",
                "id": "criteriaType",
                "options": ['Dependent', 'Independent', 'Static'],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Criteria Type is required',
                ],
                "show": true,
            },
            "table": {
                "type": "select",
                "label": "Table",
                "id": "table",
                "options": ["Table1"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Table is required',
                ],
                "show": true,
            },
            "column": {
                "type": "select",
                "label": "Column",
                "id": "column",
                "options": ["Column1"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Column is required',
                ],
                "show": true,
            },
            "joinType": {
                "type": "select",
                "label": "Join Type",
                "id": "joinType",
                "options": ["AND", "OR"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Join Type is required',
                ],
                "show": true,
            },
            "operator": {
                "type": "select",
                "label": "Operator",
                "id": "operator",
                "options": ["<", "<=", "<>", "=", ">=", ">", "EXISTS", "IN", "IS NOT NULL", "NULL", "LIKE", "NOT EXISTS", "NOT IN", "NOT LIKE"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Operator is required',
                ],
                "show": true,
            },
            "valueType": {
                "type": "select",
                "label": "Value Type",
                "id": "valueType",
                "options": ["Dependent SQL", "SQL", "VALUE"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Value Type is required',
                ],
                "show": true,
            },
            "dataType": {
                "type": "select",
                "label": "Data Type",
                "id": "dataType",
                "options": ["DATE", "NUMBER", "STRING"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Data Type is required',
                ],
                "show": true,
            },
            "format": {
                "type": "text",
                "label": "Format",
                "id": "format",
                "model": "",
                "required": false,
                "show": true,
            },
            "parameterMandatory": {
                "type": "select",
                "label": "Parameter Mandatory",
                "id": "parameterMandatory",
                "options": ["NO", "YES"],
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Parameter Mandatory is required',
                ],
                "show": true,
            },
            "link": {
                "type": "select",
                "label": "Link",
                "id": "link",
                "options": ["KB"],
                "model": "",
                "required": false,
                "show": true,
            },
            "sequence": {
                'type': "text",
                "label": "Sequence",
                "id": "sequence",
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Sequence is required',
                ],
                "show": true,
            },
            "value": {
                "type": "description_notes",
                "label": "Value",
                "id": "value",
                "model": "",
                "required": true,
                "rules": [
                    v => !!v || 'Value is required',
                ],
                "show": true,
            }
        }
    },
    {
        'title': "Notes",
        "columns": 1,
        "fields": {
            "description": {
                "type": "description_notes",
                "label": "Description",
                "id": "description",
                "required": false,
                "model": '',
                "show": true,
            }
        }
    }
]
  

我该如何实现?

     

谢谢..

2 个答案:

答案 0 :(得分:0)

解决方案:

var temp = modelValues.reduce((acc, modelValue) => Object.assign({}, acc, modelValue), {});
Object.keys(temp).forEach(key => formFields[key].model = temp[key])

注意:modelValues是您的数组

但是,我建议像这样更改您的结构?

var modelValues = {
    criteriaName: "CRITERIA 1",
    criteriaType: "Dependent",
    table: "Table1",
    column: "Column1",
    joinType: "OR",
    operator: ">=",
    valueType: "SQL",
    dataType: "NUMBER",
    format: "Config",
    parameterMandatory: "YES",
    link: "KB",
    sequence: "SEQ1",
    value: "VAL",
    description: "abcde"
};

并执行此操作以映射值

Object.keys(modelValues).forEach(key => formFields[key].model = modelValues[key])

答案 1 :(得分:0)

我相信这就是您想要的:

yourFirstArray.forEach(function(e){
    formFields.forEach(function(fF){
        var firstKey = Object.keys(e)[0]; // taking first key of each object in your first array (0)
        fF[firstKey] = e[firstKey];
    })
})