简体   繁体   English

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

[英]splice objects from arrays into new array

I have two arrays of objects which I would like to combine into a new array, splicing each object from one array into another based on the previewNumber value as the index used in .splice .我有我想从一个阵列组合成一个新的数组,每个拼接对象到另一个基于对象的两个阵列previewNumber值作为使用的索引.splice I've used .map but it only loops for the length of arrayOne , below is some example code, any help would be greatly appreciated我使用过.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", }, ]

You can achieve this using flatMap .您可以使用flatMap来实现这一点。 Here is an implementattion.这是一个实现。

 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);

Or you can convert the arrayOne to objects.或者您可以将 arrayOne 转换为对象。 Something like this:像这样的东西:

 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);

First, make the newArray copy of arrayTwo .首先,制作arrayTwonewArray副本。 After that, you could iterate through arrayOne add into newArray by splicing from previewNumber + index之后,您可以通过从previewNumber + index拼接来遍历arrayOne添加到newArray

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

console.log(newArray)

Full demo完整演示

 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