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

时间:2018-06-30 13:48:32

标签: javascript arrays dictionary

我想将此表的<div class="header"> </div>chapter_id与下面的brothers和Chapters表进行映射,并分别返回brother_idbrothername字段。使用js或jquery。我正在使用vuejs返回minutes数组作为计算属性。见下文。

name中,就像

  

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",
      ...
   },
   { ... }
]
Vue.js
[
   {
      "id":1,
      "profile_id":1,
      "chapter_id":1,
      "brothername":"Golpher",
      "firstname":"Jack",
      ...
   },
   { ... },
   {
      "id":4,
      "profile_id":4,
      "chapter_id":1,
      "brothername":"Sheera",
      "firstname":"Jake",
      ...
   }
]

3 个答案:

答案 0 :(得分:1)

我假设您不希望通过此操作对原始数组中的对象进行变异。

注意:您可能需要处理相应表中不存在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)

答案 1 :(得分:0)

这应该是您所需要的

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",
   }
]
/* code starts here */
let newMinutesTable = JSON.parse(JSON.stringify(minutesTable)).map(a => {
  let brother = brotherTable.find(id => id.id === a.brother_id);
  let chapter = chapterTable.find(id => id.id === a.chapter_id)
  if (brother) a.brother_id = brother.brothername
  if (chapter) a.chapter_id = chapter.name;
  return a;
})
console.log([minutesTable,newMinutesTable]);

答案 2 :(得分:0)

我认为您应该首先准备这些值,以便更好地理解。所以我做到了,让我来逐个解释。

您的输入信息:

var 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"
}],
    chapterTable = [{
  "id": 1,
  "letter_representation": "A",
  "name": "Alpha",
  "founded_at": "UCLA",
}],
    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",
}];

以某种方式,您将不得不将此信息视为变量。我们将为此工作。

准备数据

处理对象数组时,如果您需要从不同的数组中查找每个对象的唯一信息,尤其是要多次运行时,这有点麻烦。因此,无需使用对象数组,我们可以节省生命,将其更改为对象的对象,其中每个项目索引必须是唯一的ID。看:

var chapters = {},
    brothers = {};
chapterTable.map(function(el, i) {
    chapters[el.id] = el;
});
brotherTable.map(function(el, i) {
    brothers[el.id] = el;
});

现在,您可以轻松地按Chapter_id查找章节,或通过brother_id查找兄弟,对吗?然后,您可以完成以下问题:

var output = [];
minutesTable.map(function(el, i) {
    var item = {
        "location": el.location, // note that values are just default values, just in case
        "chapter_id":"-", 
        "brother_id":"-", 
        "created_at": el.created_at,
        "status": el.status
    };
    // PS: you need to check if that brother_id really exists!
    if(brothers[el.brother_id] != undefined) {
        item.brother_id = brothers[el.brother_id].brothername;
    }
    // PS: the same with chapters
    if(chapters[el.chapter_id] != undefined) {
        item.chapter_id = chapters[el.chapter_id].name;
    }
    output.push(item);
});

就是这样。无论如何,如果可以更改SQL查询,最好使用SQL连接并在那里准备值。