代码之家  ›  专栏  ›  技术社区  ›  sogeniusio

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

  •  0
  • sogeniusio  · 技术社区  · 8 年前

    我想把这张桌子的 chapter_id brother_id 与下面的兄弟和章节表一起返回 brothername name 分别是菲尔德。使用js或jquery。我使用Vuejs返回分钟数组作为计算属性。见下文。

    sql 有点像

    select brothername from brothers where minute.brother_id = brothers.id …然后把妓院作为新的价值观 大哥

    同样的事情 第四章 以下内容:

    从brothers中选择brothername,其中minute.brother_id=brothers.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
        }
    },
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Damon    8 年前

    我假设您不想用这个操作改变原始数组中的对象。

    注意 你可能想处理这个案子 brother_id chapter_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)
        2
  •  0
  •   Manually Overridden    8 年前

    这应该是你需要的

    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]);
        3
  •  0
  •   Kaique Garcia    8 年前

    我认为你应该先准备好这些价值观,以便更好地理解。所以我做了这个,让我分块解释。

    您的输入信息:

    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;
    });
    

    现在你可以很容易地找到一章一章或者一个兄弟一章,对吧?然后你可以像这样完成这个问题:

    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连接并在那里准备值。