嗨,我正在尝试使用把手模板,为此,我需要根据数组值创建一个json
{"path":"Avions", "fileName":"AvionsEdit.vue"},{"path":"Avions",
"fileName":"AvionsShow.vue"}ect...
我可以在代码部分返回json,但我想要类似
{"path":["Avions","Avions"],"fileName":
["AvionsEdit.vue","AvionsShow.vue"]}
var foo = {"path" : [], "fileName": []};
for(i = 0; i < list.length; i++) {
foo.path.push(list[i]);
foo.fileName.push(list[i]+extList[i]+".vue");
}
console.log(JSON.stringify(foo));
这是我的清单
['Avions',
'Avions']
这是我的extList
['Edit',
'Show']
答案 0 :(得分:0)
使用所有嵌套的for循环,使自己感觉有些过时,但我无能为力(尽管很简单易懂,但在很大程度上取决于源数据的一致性):
var srcData = {
"path": [
"somepath",
"anotherpath",
"yetanotherpath"
],
"filename": [
"somefilename",
"anotherfile",
"yetanotherfile"
]
};
const transform = src => {
let res = [];
let attributes = Object.keys(src);
for(let i = 0; i < Object.values(src)[0].length; i++){
let entry = {};
for(let j = 0; j < attributes.length; j++){
entry[attributes[j]] = Object.values(src)[j][i];
}
res.push(entry);
}
return res;
};
console.log(transform(srcData));
答案 1 :(得分:0)
我没有看到您的新评论,我正在等待一些通知,但是它永远不会到来。 我创建了一个生成所需输出的函数。 这些不同的参数是:
var path = [ // path or prefix
"path",
"otherPath",
"finalPath"
];
var files = [ // file
"Edit",
"Get",
"Foo"
];
console.log(transform(path,files,".vue"));
function transform(path, files,suffix) { // take your path, file and the suffix of your file
if (path.length == files.length) { // check if the length is the same
let results = { "path": [], "filename": [] }; // generate an object for the return
for (let i = 0; i < path.length; i++) {
results.path.push(path[i]); // push in the path array
results.filename.push(path[i]+files[i]+suffix) // push in the file name array. Concatenate the Path + File + the suffix
}
return results;
}
}