所以我有一个值为["folderid":"DTSZ", "folderid":"IEACF6FVGG", "folderid":"IEACKQC6A"]
的array1和另一个值为["title":"firsttitle", "title":"second","title":"thirdtitle"]
的数组2
现在可以说使用javascript,我想将其另存为json对象。
[
{"folderid":"DTSZ","title":"firsttitle"},
{"folderid":"IEACF6FVGG", "title":"second"},
{"folderid":"IEACKQC6A", "title":"thirdtitle"}
]
我尝试循环播放和连续播放,但无法正常工作。
array1= ["folderid":"DTSZ", "folderid":"IEACF6FVGG", "folderid":"IEACKQC6A"] ;
array2 = ["title":"firsttitle", "title":"second","title":"thirdtitle"];
使用json对象获取数组
[
{"folderid":"DTSZ","title":"firsttitle"},
{"folderid":"IEACF6FVGG", "title":"second"},
{"folderid":"IEACKQC6A", "title":"thirdtitle"}
]
答案 0 :(得分:0)
在JavaScript中,数组仅包含值,在您的示例中,该数组无效,因为您尝试添加直接的key: values
元素。即
["folderid":"DTSZ"] // invalid !! (notice semicolon)
["folderid", "DTSZ"] // VALID (notice comma)
如果要转换为有效数组然后转换为对象,则可以使用诸如条目之类的东西,它们是数组的数组。
让我们以第一个示例为例,将其转换为条目:
const arr1 = [["folderid", "DTSZ"], ["folderid", "IEACF6FVGG"], ["folderid","IEACKQC6A"]]
然后将其转换为对象,您可以像这样使用Object.fromEntries:
const obj1 = Object.fromEntries(entries);
因此,首先要集中精力将您最初的无效数组转换为条目,然后完成工作!
答案 1 :(得分:0)
使用以下代码。
var a = [{ "folderid": "DTSZ" }, { "folderid": "IEACF6FVGG" }, { "folderid": "IEACKQC6A" }]
var b = [{ "title": "firsttitle" }, { "title": "second" }, { "title": "thirdtitle" }]
var newObject = a.map((o, index) => {
const temp = Object.assign(o, b[index]);
return temp;
});
console.log('output ---- ', newObject)