如何解决将具有不同对象类型的两个不同数组合并为一个
我想将具有不同对象类型的两个不同数组合并为一个。
var column = [
{
"href": "./?course=13&sort=firstname&silast=all&sifirst=all","title": null
},{
"href": undefined,"title": undefined
},{
"href": "http://someurl.edu.lk/mod/questionnaire/view.PHP?id=304","title": "Survey on student familiarity with the online learning platform"
},{
"href": "http://someurl.edu.lk/mod/assign/view.PHP?id=307","title": "update your profile"
}
]
和另一个数组为:
var obj = [
{
name: "POD DLK",profile: "http://someurl.edu.lk/user/view.PHP?id=217&course=103"
},{
name: "BGC DO",profile: "http://someurl.edu.lk/user/view.PHP?id=218&course=103"
},{
name: "CAD SG",profile: "http://someurl.edu.lk/user/view.PHP?id=219&course=103"
},{
name: "BON DTH",profile: "http://someurl.edu.lk/user/view.PHP?id=207&course=103"
}
]
我曾经尝试过使用:
const mergedArray = [ ...column,...obj]
console.log(mergedArray)
但这会在 mergedArray 中创建一个单独的对象。
预期输出:
[{
href: "./?course=13&sort=firstname&silast=all&sifirst=all",title: null,name: "POD DLK",profile: "http://someurl.edu.lk/user/view.PHP?id=217&course=103"
},{
href: undefined,title: undefined,name: "BGC DO",profile: "http://someurl.edu.lk/user/view.PHP?id=218&course=103"
},{
href: "http://someurl.edu.lk/mod/questionnaire/view.PHP?id=304",title: "Survey on student familiarity with the online learning platform",name: "CAD SG",profile: "http://someurl.edu.lk/user/view.PHP?id=219&course=103"
},{
href: "http://someurl.edu.lk/mod/assign/view.PHP?id=307",title: "update your profile",name: "BON DTH",profile: "http://someurl.edu.lk/user/view.PHP?id=207&course=103"
}]
解决方法
您需要按相同的索引合并。
const
column = [{ href: "./?course=13&sort=firstname&silast=all&sifirst=all",title: null },{ href: undefined,title: undefined },{ href: "http://someurl.edu.lk/mod/questionnaire/view.php?id=304",title: "Survey on student familiarity with the online learning platform" },{ href: "http://someurl.edu.lk/mod/assign/view.php?id=307",title: "update your profile" }],obj = [{ name: "POD DLK",profile: "http://someurl.edu.lk/user/view.php?id=217&course=103" },{ name: "BGC DO",profile: "http://someurl.edu.lk/user/view.php?id=218&course=103" },{ name: "CAD SG",profile: "http://someurl.edu.lk/user/view.php?id=219&course=103" },{ name: "BON DTH",profile: "http://someurl.edu.lk/user/view.php?id=207&course=103" }],result = column.map((o,i) => ({ ...o,...obj[i] }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
使用reduce的明显不同的方法
const
column = [{ href: "./?course=13&sort=firstname&silast=all&sifirst=all",result = [column,obj].reduce((r,a) => a.map((o,i) => ({ ...r[i],...o })),[]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
,
第一个将两个数组成对组合的辅助函数:
const zip = (a,b) => {
const results = [];
// We'll merge the arrays 'a' and 'b' to the length
// of the shorter one so we don't get an error if they
// are different lengths
for (let i = 0; i < Math.min(a.length,b.length); ++i) {
result.push([a[i],b[i]);
}
return results;
};
然后由于解构,合并成对的对象成为一个不错的选择:
const merged = zip(obj,column).map(([x,y]) => ({ ...x,...y }));
,
column.forEach((value,index)=>{Object.assign(value,obj[index])})
如果要新数组:
column.forEach((value,index)=>{result.push({...value,...obj[index]})})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。