【问题标题】:splice objects from arrays into new array将数组中的对象拼接到新数组中
【发布时间】:2020-09-28 00:24:53
【问题描述】:

我有两个对象数组,我想将它们组合成一个新数组,根据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",
  },
]

【问题讨论】:

  • 只要const mergedArr = [...arrayOne, ...arrayTwo]。从所需的输出中,我了解到您正在寻找合并两个数组。
  • 我意识到我错过了一个关键细节,我想使用previewNumber 值将arrayOne 中的对象拼接到arrayTwo 顺序中,但创建一个新数组而不是改变arrayTwo
  • arrayOne 是否按previewNumber 排序?
  • @LLawliet 不,我想在与arrayTwo 结合时使用previewNumber 的值作为新数组中的索引。

标签: javascript


【解决方案1】:

您可以使用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);

【讨论】:

    【解决方案2】:

    首先,制作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)

    【讨论】:

    • @gorak 感谢您的纠正。包含减一是多余的
    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 2017-01-09
    • 1970-01-01
    • 2019-10-16
    • 2021-06-01
    • 2022-11-20
    • 2021-08-29
    • 2020-12-25
    相关资源
    最近更新 更多