在id - map函数中合并两个数组 - Javascript

时间:2017-07-26 19:22:21

标签: javascript arrays

我正在尝试在id(sourceID)上合并两个数组。但没有这样做。第一个数组如下:

eventsToBeInserted = [{sourceID: 1, name: "BettyNansen"}, {sourceID: 2, name: "kongenshave"}]

这是第二个数组:

images = [{sourceID: 1, images: "Bettynansen.jpg"}, {sourceID: 2, name: "kongenshave.jpg"}]

我希望得到以下内容:

events = [{sourceID: 1, name: "BettyNansen", images: "Bettynansen.jpg"}, {sourceID: 2, name: "kongenshave", images: "kongenshave.jpg"}]

这是我的代码:



const eventsToBeInserted = [{
  sourceID: 1,
  name: "BettyNansen"
}, {
  sourceID: 2,
  name: "kongenshave"
}]

const images = [{
  sourceID: 1,
  images: "Bettynansen.jpg"
}, {
  sourceID: 2,
  name: "kongenshave.jpg"
}]

events: !!eventsToBeInserted ? eventsToBeInserted.sourceID.map(sourceID => {
  const event = images.name.find(eventLookup => eventLookup.sourceID === sourceID);
  return [eventsToBeInserted.name, images.name]
}) : []




1 个答案:

答案 0 :(得分:3)

基本上你想要的是,对于eventsToBeInserted中的每个元素,找到images数组中属于它的所有图像。

注意:在images数组中,第一个元素具有属性images,第二个元素具有属性name

const images = [{
  sourceID: 1,
  images: "Bettynansen.jpg"
// ^
}, {
  sourceID: 2,
  name: "kongenshave.jpg"
// ^
}]

两者都应该使用name似乎合乎逻辑,以便我的例子如下所示。如果这些对象可以同时使用imagesname,则在检索值时,您应该按照首选的顺序||这两个属性。



const eventsToBeInserted=[{sourceID:1,name:"BettyNansen"},{sourceID:2,name:"kongenshave"}],images=[{sourceID:1,name:"Bettynansen.jpg"},{sourceID:2,name:"kongenshave.jpg"}];

console.log(
  eventsToBeInserted.map(
    event => Object.assign({}, event, {
      images: images
        .filter(img => img.sourceID === event.sourceID)
        .map(img => img.name)
    })
  )
)




如果每个对象始终只匹配1张图片,则根据您的示例,您可以在find数组上使用filter代替images

注意:如果您只有一个图片,那么将该属性命名为image而不是images会更有意义。



const eventsToBeInserted=[{sourceID:1,name:"BettyNansen"},{sourceID:2,name:"kongenshave"}],images=[{sourceID:1,name:"Bettynansen.jpg"},{sourceID:2,name:"kongenshave.jpg"}];

console.log(
  eventsToBeInserted.map(
    event => Object.assign({}, event, {
      image: (
        images.find(img => img.sourceID === event.sourceID) || {}
      ).name
    })
  )
)




如果此操作需要多次执行,则将images转换为sourceID: imageName的哈希值会更有效。



const eventsToBeInserted=[{sourceID:1,name:"BettyNansen"},{sourceID:2,name:"kongenshave"}],images=[{sourceID:1,name:"Bettynansen.jpg"},{sourceID:2,name:"kongenshave.jpg"}];

const imagesHash = images.reduce((hash, img) => {
  hash[img.sourceID] = img.name;
  return hash;
}, {});

console.log(
  eventsToBeInserted.map(
    event => Object.assign({}, event, {
      image: imagesHash[event.sourceID]
    })
  )
)




最后,假设您的环境允许,您可以使用object spread来简化语法:



const eventsToBeInserted=[{sourceID:1,name:"BettyNansen"},{sourceID:2,name:"kongenshave"}],images=[{sourceID:1,name:"Bettynansen.jpg"},{sourceID:2,name:"kongenshave.jpg"}];

const imagesHash = images.reduce((hash, img) => {
  hash[img.sourceID] = img.name;
  return hash;
}, {});

console.log(
  eventsToBeInserted.map(
    event => ({
      ...event,
      image: imagesHash[event.sourceID]
    })
  )
)