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

索格纽西奥

我想将这个表的chapter_idand映射到brother_id下面的兄弟和章节表,并分别返回brothernamename字段。使用 js 或 jquery。我正在使用 vuejs 返回分钟数组作为计算属性。见下文。

sql它是这样的

select brothername from brothers where minute.brother_id = brothers.id ...然后将兄弟名称设置为新值 brother_id

同样的事情也适用于chapter_id

select brothername from brothers where minute.brother_id = brothers.id ...然后将兄弟名称设置为新值 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",
      ...
   },
   { ... }
]
兄弟的桌子
[
   {
      "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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用PHP将第二个数组值插入第一个数组

KnockOut.JS比较两个数组并从第二个数组中删除唯一值

尝试使用reducer函数将一个数组中的所有值添加到JS中的另一个数组中

遍历2个包含对象的数组,并使用第一个arrays键返回第二个数组的值

使用指针将第一个(二维)数组的值复制到第二个数组的函数

使用 numpy,如何生成一个数组,其中每个索引的值是第二个数组中从 0 到同一索引的值的总和?

如何将多个数组推送到另一个数组并在 React JS 中正确使用 useEffect 更新值?

Php,使用第一个键和第二个值连接两个数组

使用Javascript验证第二个数组中的值是否与第一个数组的平方值对应的函数

如何使用包含将一个数组的属性值与另一个数组逻辑应用程序表达式进行比较?

Javascript:使用第二个数组中的值提取数组中的密钥对值

JS,通过一个数组映射,当它与另一个数组具有相同的值时进行过滤

使用另一个数组中的值返回一个数组中的数组

VueJS2:如何提取数组的一个属性并使用它在第二个数组中查找匹配值?

使用另一个数组中的值过滤一个数组

将第一个数组值与第二个数组键进行比较并替换值

如何在PHP中对一个数组进行排序并使用键顺序对第二个数组进行排序?

如何在MATLAB中对一个数组进行排序并使用命令对第二个数组进行排序

将两个数组与PHP结合使用。第二个数组是多维的

如果值存在,则使用另一个数组对数组进行排序

比较两个数组并替换/合并另一个数组 (js) 中存在的 id 值

在PHP中使用for循环将数组值存储在另一个数组中

使用numpy数组将值分配给另一个数组

使用 Key 将数组值推送到另一个数组

使用指针算术将数据从第一个数组复制到第二个数组

Vue JS使用数组过滤通过另一个数组吗?

使用第二个数组作为参考对numpy数组的元素进行分类

使用另一个数组中的特定值过滤多个数组

使用比较器无法根据另一个数组对一个数组进行排序