问题描述
这是对象的开始。我应该能够得到celldata.values
的元素。
var generatedobj1 = {
"agg": "Glossar","ifa": null,"ifv": null,"ird": null,"afv": null,"ard": null,"MetaData": {
"cellMetaDataList": [{
"cell": "glossar",// <-- cell type
"cmv": null,"crd": null,"logicalData": {
"body": [{
"id": "f5d",// <-- body id 1
"name": "name",// <-- attribute name 1
"minLength": 3,"maxLength": 100,"minValue": null,"maxValue": null,"wertebereich": "ALPHANUMERISCH","validationCode": "","exampleValues": ["Langer Name","Kurzer Name","Kein Name"]
},{
"id": "42",// <-- body id 2
"name": "kurzname",// <-- attribute name 2
"minLength": 3,"maxLength": 20,"exampleValues": ["long_desc","short_desc","no_desc"]
},{
"id": "9d",// <-- body id 3
"name": "eindeutig",// <-- attribute name 3
"minLength": 1,"maxLength": 1,"wertebereich": "WAHRHEITSWERT","exampleValues": [true,false]
}
]
}
}]
},"data": {
"cellData": [{ // row 1 from res1
"cell": "glossar",// <-- cell type
"id": "5b",// <- UUID v4 (generated by the function)
"cmv": null,"caption": {
"pers": "string","custNr": "string","fnam": "string","snam": "string","sysId": "string"
},"values": [{
"name": "name",// <-- attribute name 1
"value": "Langer Name","bodyRefId": "5d" // <-- body id 1
},{
"name": "kurzname",// <-- attribute name 2
"value": "no_desc","bodyRefId": "42" // <-- body id 2
},{
"name": "eindeutig",// <-- attribute name 3
"value": false,"bodyRefId": "9d" // <-- body id 3
}
]
},{ // row 2 from res1
"cell": "glossar",// <-- cell type
"id": "5c",// <-- attribute name 1
"value": "Kein Name",// <-- attribute name 2
"value": "short_desc",// <-- attribute name 3
"value": true,{ // row 3 from res1
"cell": "glossar",// <-- cell type
"id": "5d",// <-- attribute name 1
"value": "Kurzer Name","bodyRefId": "9d" // <-- body id 3
}
]
}
]
}
}
Blockquote
应该转换返回的数组以匹配元数据。 数组中的每个条目都描述了结果对象中的一个对象。 并且应该能够更改实现以下功能中的要求。
function add(input1) {
let exampleValues1 = input1.MetaData.cellMetaDataList[0].logicalData.body.map(({
exampleValues
}) => exampleValues);
return comb(exampleValues1,20);
}
组合功能。
function comb(args,output) {
var combination = [],max = args.length-1;
function helper(arr,i) {
for (var j=0,l=args[i].length; j<l; j++) {
var a = arr.slice(0); // clone arr
a.push(args[i][j]);
if (i==max)
combination.push(a);
else
helper(a,i+1);
}
}
helper([],0);
while(combination.length < output){
combination = combination.concat(combination)}
return combination.sort(function() {
return .5 - Math.random();
}).slice(0,output); }
console.log(add(generatedobj1));
抽样结果:
[["Langer Name","no_desc",false],["Kein Name",true]
["Kurzer Name",true]
];
应该转换返回的数组以匹配元数据。 数组中的每个条目都描述了结果对象中的一个对象。
您仍然可以只调用add函数并获取输入信息 输入根本不会改变,我们只是在更改输出...(这就是票证被命名为输出结构的改编的原因)
对于comb函数输出中的每个字符串,add函数应在cellData元素内创建一个对象。它应具有单元格类型,id,空cmv和crd值以及标题对象 最重要的是,它应该具有一个值数组,该数组具有一个对象,该对象带有属性名称(来自body.name),值(来自返回的数组梳)和bodyRefId(来自body.id)
解决方法
您可以通过使用方括号符号填充对象的字段来从数组创建对象。该方法将如下所示:
function mapArrayToObject(cellDataValues) {
const obj = {};
cellDataValues.forEach(element => {
const fieldName = element[0];
const fieldValue = element[2];
obj[fieldName] = fieldValue;
});
return obj;
}
更新09/17/2020(这个问题被误解了)
在仔细研究了您的问题之后,我猜您的目标是下一个目标:在执行add函数之后,您想要获得的是一个包含某些字段(id
,cell
,{ {1}} ...)和一个caption
字段,其中包含由values
函数计算的值,但被解析为包含有关comb
信息的对象。
为了实现这一目标,我打算在body
函数内部分三个步骤进行解决:
- 使用与
add
函数所计算的值无关的所有字段来创建对象。 - 执行
comb
函数。 - 使用在第一步中创建的对象作为模板(克隆它)来遍历获得的值,并使用要遍历的值的索引来知道您所使用的
comb
指向以获取每个值的body
和name
。
实现为:
bodyRefId