簡體   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