比较基于id的两个数组并根据值

时间:2018-03-28 20:19:28

标签: javascript jquery loops lodash

尝试遍历两个数组并找到每个数组的代码,我可能会遗漏某些部分。我需要用这个值构造一个新数组,每个id在arrayB中可以多次。基于第一个数组中的id,我们必须匹配第二个数组(arrayA)中的id并获取代码

let arrayA=[
    {"breadcrumb":{id: "abdc4051"}, type:"details"},
    {"breadcrumb":{id: "abdc4052"}, type:"details"},

    let arrayB=[
    {"breadcrumb": {id: "abdc4051",code: "mike", length:"short"}},
    {"breadcrumb": {id: "abdc4051", code: "pohan", length:"long"}}, {"breadcrumb": {id: "abdc4052", code: "junior", length:"short"}}]

    let arrayC = [];
    // output expected
[{"id":"abdc4051", shortLength: "mike", longLength:"pohan"}, {"id":"abdc4052", shortLength: "Junior", longLength:"-"}]
    // tried this
    function isBiggerThan10(element, index, array) {
      return element > 10;
    }

    arrayA.forEach(function(element){
      arrayC.push({
      id:element.id,
      firstName:(arrayB.find(
        e => e.attributes.code==="mike")).breadCrumbs.shortLength,
      lastName:(arrayB.find(
        e => e.code==="pohan")).breadCrumbs.longlength
      })
    });

    console.log(arrayC);

2 个答案:

答案 0 :(得分:1)

这是使用内置数组方法的一种解决方案。步骤是:

对于arrayA中的每个项目,请执行以下操作:

  • 查找arrayB中具有相同ID的所有项目(使用.filter)。
  • 将所有这些结果合并到一个对象中,如果不存在,则使用默认的“ - ”表示名字和姓氏(使用.reduce)。
  • 将其放入结果数组中(首先使用.map上的arrayA处理)。

let arrayA = [
    {"breadcrumb":{id: "abdc4051"}, type:"details"},
    {"breadcrumb":{id: "abdc4052"}, type:"details"},
]

let arrayB = [
    {"breadcrumb": {id: "abdc4051", firstName: "mike"}},
    {"breadcrumb": {id: "abdc4051", lastName: "pohan"}},
    {"breadcrumb": {id: "abdc4052", firstName: "junior"}},
]

// output expected
// [
//   {"id":"abdc4051", firstName: "mike", lastName:"pohan"},
//   {"id":"abdc4052", firstName: "Junior", lastName:"-"},
// ]

const result = arrayA.map(itemA => {
    return arrayB
        .filter(itemB => itemB.breadcrumb.id === itemA.breadcrumb.id)
        .reduce((combo, item) => ({...combo, ...item.breadcrumb}), {firstName: "-", lastName: "-"})
});

console.log(result);

编辑:根据已修改的问题,您可以修改reduce功能,以查看组合是否设置了firstCode。如果是,请将下一个code置于密钥lastCode下,否则将其保留为firstCode。这将使第一个/最后一个代码基于它们在arrayB中出现的顺序:

let arrayA = [
    {"breadcrumb":{id: "abdc4051"}, type:"details"},
    {"breadcrumb":{id: "abdc4052"}, type:"details"},
]

let arrayB = [
    {"breadcrumb": {id: "abdc4051", code: "mike"}},
    {"breadcrumb": {id: "abdc4051", code: "pohan"}},
    {"breadcrumb": {id: "abdc4052", code: "junior"}},
]

// output expected
// [
//   {"id":"abdc4051", firstCode: "mike", lastCode:"pohan"},
//   {"id":"abdc4052", firstCode: "Junior", lastCode:"-"},
// ]

const result = arrayA.map(itemA => {
    return arrayB
        .filter(itemB => itemB.breadcrumb.id === itemA.breadcrumb.id)
        .reduce((combo, item) => ({...combo, [combo.firstCode === "-" ? "firstCode" : "lastCode"]: item.breadcrumb.code, id: itemA.breadcrumb.id}), {firstCode: "-", lastCode: "-"})
});

console.log(result);

编辑2:每秒编辑,您可以再次修改缩小以满足您的需求:

let arrayA = [
    {"breadcrumb":{id: "abdc4051"}, type:"details"},
    {"breadcrumb":{id: "abdc4052"}, type:"details"},
]

let arrayB = [
    {"breadcrumb": {id: "abdc4051", code: "mike", length: "short"}},
    {"breadcrumb": {id: "abdc4051", code: "pohan", length: "long"}},
    {"breadcrumb": {id: "abdc4052", code: "junior", length: "short"}},
]

// output expected
// [
//   {"id":"abdc4051", shortLength: "mike", longLength:"pohan"},
//   {"id":"abdc4052", shortLength: "junior", longLength:"-"},
// ]

const result = arrayA.map(itemA => {
    return arrayB
        .filter(itemB => itemB.breadcrumb.id === itemA.breadcrumb.id)
        .reduce((combo, item) => ({...combo, [item.breadcrumb.length + "Length"]: item.breadcrumb.code, id: itemA.breadcrumb.id}), {shortLength: "-", longLength: "-"})
});

console.log(result);

答案 1 :(得分:1)

使用arrayB开始一个链。使用_.map()breadcrumb获取_.groupBy()id的内容。将_.pick()arrayA的ID一起使用即可获得所需的群组。使用_.transform()将每个组映射到所需的表单:

const arrayA=[{"breadcrumb":{"id":"abdc4051"},"type":"details"},{"breadcrumb":{"id":"abdc4052"},"type":"details"}];
const arrayB=[{"breadcrumb":{"id":"abdc4051","code":"mike","length":"short"}},{"breadcrumb":{"id":"abdc4051","code":"pohan","length":"long"}},{"breadcrumb":{"id":"abdc4052","code":"junior","length":"short"}}];
    
const result = _(arrayB)
  .map('breadcrumb') // unwrap breadcrumb
  .groupBy('id')
  .pick(arrayA.map((o) => _.get(o, 'breadcrumb.id'))) // get all groups that match arrayA ids
  .map((g, key) => _.transform(g, (acc, v) => { // transform each group to the requested form
    acc[`${v.length}Length`] = v.code;
  }, { key, shortLength: '-', longLength: '-' }))
  .value(); 
  
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>