繁体   English   中英

将数组中的对象拼接到新数组中

[英]splice objects from arrays into new array

我有我想从一个阵列组合成一个新的数组,每个拼接对象到另一个基于对象的两个阵列previewNumber值作为使用的索引.splice 我使用过.map但它只循环arrayOne的长度,下面是一些示例代码,任何帮助将不胜感激

 const arrayOne = [ { "title": "A Project", "slug": "a-project", "previewNumber": 2, }, { "title": "New Project", "slug": "new-project", "previewNumber": 4, } ] const arrayTwo = [ { "title": "Project 5546", "slug": "project-5546", }, { "title": "Project 456", "slug": "project-456", }, { "title": "Rand Project 467", "slug": "rand-project-467", }, { "title": "Random Project 245", "slug": "random-project-245", }, { "title": "Example Project", "slug": "example-project", }, ] const newArray = arrayOne.map((item) => arrayTwo.splice(item.previewNumber, 0, item) ); console.log(newArray) const desiredOutput = [ { "title": "Project 5546", "slug": "project-5546", }, { "title": "Project 456", "slug": "project-456", }, { "title": "A Project", "slug": "a-project", "previewNumber": 2, }, { "title": "Rand Project 467", "slug": "rand-project-467", }, { "title": "Random Project 245", "slug": "random-project-245", }, { "title": "New Project", "slug": "new-project", "previewNumber": 4, }, { "title": "Example Project", "slug": "example-project", }, ]

您可以使用flatMap来实现这一点。 这是一个实现。

 const arrayOne = [ { "title": "A Project", "slug": "a-project", "previewNumber": 2, }, { "title": "New Project", "slug": "new-project", "previewNumber": 4, }]; const arrayTwo = [ { "title": "Project 5546", "slug": "project-5546", }, { "title": "Project 456", "slug": "project-456", }, { "title": "Rand Project 467", "slug": "rand-project-467", }, { "title": "Random Project 245", "slug": "random-project-245", }, { "title": "Example Project", "slug": "example-project", }]; const result = arrayTwo.flatMap((p,i)=>{ position = arrayOne.find(k=>k.previewNumber===i); return position ? [position, p] : p }); console.log(result);

或者您可以将 arrayOne 转换为对象。 像这样的东西:

 const arrayOne = [ { "title": "A Project", "slug": "a-project", "previewNumber": 2, }, { "title": "New Project", "slug": "new-project", "previewNumber": 4, }]; const mapped = Object.fromEntries(arrayOne.map(p=>[p.previewNumber,p])); const arrayTwo = [ { "title": "Project 5546", "slug": "project-5546", }, { "title": "Project 456", "slug": "project-456", }, { "title": "Rand Project 467", "slug": "rand-project-467", }, { "title": "Random Project 245", "slug": "random-project-245", }, { "title": "Example Project", "slug": "example-project", }]; const result = arrayTwo.flatMap((p,i)=>mapped[i] ? [mapped[i], p] : p); console.log(result);

首先,制作arrayTwonewArray副本。 之后,您可以通过从previewNumber + index拼接来遍历arrayOne添加到newArray

const newArray = [...arrayTwo]
arrayOne.forEach((el, index) => {
  newArray.splice(el.previewNumber + index, 0, el)
})

console.log(newArray)

完整演示

 const arrayOne = [ { title: "A Project", slug: "a-project", previewNumber: 2, }, { title: "New Project", slug: "new-project", previewNumber: 4, }, ] const arrayTwo = [ { title: "Project 5546", slug: "project-5546", }, { title: "Project 456", slug: "project-456", }, { title: "Rand Project 467", slug: "rand-project-467", }, { title: "Random Project 245", slug: "random-project-245", }, { title: "Example Project", slug: "example-project", }, ] const newArray = [...arrayTwo] arrayOne.forEach((el, index) => { newArray.splice(el.previewNumber + index, 0, el) }) console.log(newArray)

暂无
暂无

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

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