如何在javascript中将一个数组中的值添加到另一个数组
Posted
技术标签:
【中文标题】如何在javascript中将一个数组中的值添加到另一个数组【英文标题】:How to add values from one array to another array in javascript 【发布时间】:2019-04-30 05:34:55 【问题描述】:我正在尝试将下面数组中的值添加到另一个数组中
[
"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 元素数组的方法也适用于更长的数组。跨度> 【参考方案1】:我相信这就是你想要的:
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];
)
)
【讨论】:
【参考方案2】:解决方案:
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])
【讨论】:
以上是关于如何在javascript中将一个数组中的值添加到另一个数组的主要内容,如果未能解决你的问题,请参考以下文章
在JavaScript中将一个数组附加到另一个数组[重复]