需要使用给定的对象创建特定的对象数组。 现在,我已经使用了assign对象。
这里是
var str = [{componentName: "Transformer", type: "PUB", beanName: "TransformerExtractor", state: "enabled", config: {"test":"test"}},{componentName: "DBEE", type: "PUBSUB", beanName: "DBEErExtractor", state: "enabled", config: {"test":"test"}}];
var A1 = [];
var B1 = [];
finalOutput = [];
for(i=0;i<str.length;i++){
alert(str.length);
var output = {};
output.flowId = "Flow1234";
output.flowAlias = "Flow1234";
output.description = "Flow1234";
output.state = "Enabled";
A1.push(output);
alert (A1);
B1.push(str[i]);
alert (B1);
finalOutput = [A1, B1].map(o => Object.assign({}, A1, o));
}
alert (finalOutput);
return finalOutput;
能否请您告诉我如何获得预期的结果,例如[{A1,B1(来自变量str的第一组),{A1,B1(来自变量str的第二组)]
答案 0 :(得分:2)
从上次编辑中获取数据,您可以为要添加到数组中的对象的数据定义模板对象。
var data = [{ componentName: "Transformer", type: "PUB", beanName: "TransformerExtractor", state: "enabled", config: { test: "test" } },{ componentName: "DBEE", type: "PUBSUB", beanName: "DBEErExtractor", state: "enabled", config: { test: "test" } }],
template = { flowId: "Flow1234", flowAlias: "Flow1234", description: "Flow1234", state: "Enabled" },
result = data.map(o => Object.assign({}, template, o));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
如果只想合并它们,可以使用lodash
data = _.merge(A1, B1)