javascript - 使用 JS 的数组映射 - 将值与另一个数组进行比较并从第二个数组返回值

标签 javascript arrays dictionary

我想将此表的 chapter_idbrother_id 与下面的兄弟和章节表进行映射,并返回 brothername name 字段分别。使用js或jquery。我正在使用 vuejs 返回分钟数组作为计算属性。见下文。

sql中,它类似于

select brothername from brothers where minute.brother_id = brothers.id ... and then set the brothername as the new value for brother_id

chapter_id 也是如此:

select brothername from brothers where minute.brother_id = brothers.id ... and then set the brothername as the new value for brother_id

生成的数组或对象应该是:

预期数组
[
   {
      "location":"UCLA",
      "chapter_id":"Beta", 
      "brother_id":"Golpher", 
      "created_at":"2008-05-15 22:23:00",
      "status":"Approved"
   },
   { ... },
   {
      "location":"John's Deli",
      "chapter_id":"Beta", notice the change in the array based on the ids
      "brother_id":"Sheera", notice the change in the array based on the ids
      "created_at":"2008-05-15 22:23:00",
      "status":"Approved"
   }
]
分钟表(原始数组)
[
   {
      "location":"UCLA",
      "chapter_id":2,
      "brother_id":1,
      "created_at":"2008-05-15 22:23:00",
      "status":"Approved"
   },
   { ... },
   {
      "location":"John's Deli",
      "chapter_id":2,
      "brother_id":4,
      "created_at":"2008-05-15 22:23:00",
      "status":"Approved"
   }
]
章节表
[
   {
      "id":1,
      "letter_representation":"A",
      "name":"Alpha",
      "founded_at":"UCLA",
      ...
   },
   { ... }
]
兄弟的 table
[
   {
      "id":1,
      "profile_id":1,
      "chapter_id":1,
      "brothername":"Golpher",
      "firstname":"Jack",
      ...
   },
   { ... },
   {
      "id":4,
      "profile_id":4,
      "chapter_id":1,
      "brothername":"Sheera",
      "firstname":"Jake",
      ...
   }
]
Vue.js
computed: {
    brothers () {
        return this.$store.state.brothers
    },
    chapters () {
        return this.$store.state.chapters
    },
    minutes () {
        return this.$store.getters.model
    }
},

最佳答案

我假设您不想通过此操作改变原始数组中的对象。

注意您可能需要处理相应表中不存在brother_idchapter_id的情况。在下面的示例中,它只是将属性值设置为 undefined

const minutesTable = [{
  "location": "UCLA",
  "chapter_id": 2,
  "brother_id": 1,
  "created_at": "2008-05-15 22:23:00",
  "status": "Approved"
}, {
  "location": "John's Deli",
  "chapter_id": 2,
  "brother_id": 4,
  "created_at": "2008-05-15 22:23:00",
  "status": "Approved"
}]
const chapterTable = [{
  "id": 1,
  "letter_representation": "A",
  "name": "Alpha",
  "founded_at": "UCLA",
}]
const brotherTable = [{
  "id": 1,
  "profile_id": 1,
  "chapter_id": 1,
  "brothername": "Golpher",
  "firstname": "Jack",
}, {
  "id": 4,
  "profile_id": 4,
  "chapter_id": 1,
  "brothername": "Sheera",
  "firstname": "Jake",
}]

// your result
const result = minutesTable.map(m => {
  const brother = brotherTable.find(b => b.id === m.brother_id)
  const chapter = chapterTable.find(c => c.id === m.chapter_id)

  return Object.assign({}, m, {
    brother_id: brother && brother.brothername,
    chapter_id: chapter && chapter.name,
  })
})

console.log(result)

关于javascript - 使用 JS 的数组映射 - 将值与另一个数组进行比较并从第二个数组返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51115531/

相关文章:

javascript - 使用 AJAX 发布帖子后返回一个值,以便在其他代码中使用该值

javascript - 使用 JavaScript 中的 onclick 函数将数据添加到表中

Python如何做列表字典的字典的.values().values()

ios - 从给定值的字典中删除键

python - 使用 Dictionary get 方法返回空列表默认​​返回 None

javascript - Android 自定义事件转 Javascript EventListener

java - 如何在java中选择合适的数组来存储XML数据

javascript - 将 FOR 循环的结果存储为一个数组(数组仅单独保存每个结果)

java - 无法创建我自己的自定义异常(exception)。请帮我

javascript - 解释Javascript代码,填充到字符串的左侧或右侧