繁体   English   中英

将具有不同对象类型的两个不同数组合并为一个

[英]merge two different arrays having different object types into one

我想将具有不同对象类型的两个不同数组合并为一个。

假设我有一个数组:

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)

但这在mergeArray 中创建了一个单独的对象。

但是,我想将obj中的第一个对象合并到column 中的第一个对象。

预期输出:

[{
  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", 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, 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]})})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM