需要将一个数组中的对象值与另一个数组中的对象值进行匹配

时间:2018-05-05 21:59:51

标签: javascript node.js typescript

在以下代码中。我正在尝试获取与selected对象匹配的变体的id

const selected = [ { "id": 14 }, { "id": 95 } ]

const variants = [
  {
    "id": 1,
    "option_values": [ { "id": 7 }, { "id": 95, } ]
  },
  {
    "id": 2,
    "option_values": [ { "id": 8 }, { "id": 95, } ]
  },
  {
    "id": 3,
    "option_values": [ { "id": 14 }, { "id": 95, } ]
  }
]

function filterVaiant() {
    return variants.filter( options => {
        // return id 3 because it matches the selected objects
    });
}

console.log(filterVaiant());

过滤器函数应返回变量id:3,因为它具有与所选选项值相同的选项值。

跟进

const colors = require('colors');

const selected = [ { "id": 14 }, { "id": 95 }, { "id": 21 } ]

let selected_ids  = selected.map(e=>e.id);

const variants = [
  {
    "id": 1,
    "option_values": [ { "id": 7 }, { "id": 95, } ]
  },
  {
    "id": 2,
    "option_values": [ { "id": 8 }, { "id": 95, } ]
  },
  {
    "id": 3,
    "option_values": [ { "id": 14 }, { "id": 95, } ]
  },
  {
    "id": 4,
    "option_values": [ { "id": 14 }, { "id": 95, }, { "id": 21 } ]
  }
]

let vID = variants.filter(e=> e.option_values.every(e=> selected_ids.indexOf(e.id) > -1));

console.log("answer:",vID) // Returns 3 and 4 but should only return 4

在这种情况下vID是3和4,但应该只返回4,因为它是唯一一个完全匹配选择的。

4 个答案:

答案 0 :(得分:2)

对于每个对象,通过检查长度是否相等,并使用{检查selected中的所有项目是否出现在option_valuesops别名)中{3}}和Array.every()

const selected = [ { "id": 14 }, { "id": 95 } ]

const variants = [{"id":1,"option_values":[{"id":7},{"id":95}]},{"id":2,"option_values":[{"id":8},{"id":95}]},{"id":3,"option_values":[{"id":14},{"id":95}]}];

function filterVaiant() {
  return variants.filter(({ option_values: ops }) => {
    if (selected.length !== ops.length) return false;
    return selected.every(({ id }) => 
      ops.find((o) => id === o.id));
  });
}

console.log(filterVaiant());

答案 1 :(得分:2)

您可以使用.filter.every来实现此目标

const selected = [ { "id": 14 }, { "id": 95 } ];

var ids  = selected.map(e=>e.id);


const variants = [
  {
    "id": 1,
    "option_values": [ { "id": 7 }, { "id": 95, } ]
  },
  {
    "id": 2,
    "option_values": [ { "id": 8 }, { "id": 95, } ]
  },
  {
    "id": 3,
    "option_values": [ { "id": 14 }, { "id": 95, } ]
  }
];

var o = variants.filter(e=> e.option_values.every(e=> ids.indexOf(e.id) > -1));

console.log(o)

这里发生的是,在数组中获取所有选定的ID,然后根据variants的每个条目是否具有option_values ID的所有条目来过滤selected数组。

答案 2 :(得分:2)

使用Set来存储所选的Id以在过滤器中查找和Array#



const selected = [ { "id": 14 }, { "id": 95 } ]

const variants = [
  {
    "id": 1,
    "option_values": [ { "id": 7 }, { "id": 95, } ]
  },
  {
    "id": 2,
    "option_values": [ { "id": 8 }, { "id": 95, } ]
  },
  {
    "id": 3,
    "option_values": [ { "id": 14 }, { "id": 95, } ]
  }
]

function filterVaiant(selected) {
    let ids = new Set(selected.map(({id})=>id))
    return variants.filter( o => o.option_values.every(({id})=>ids.has(id)));
}

console.log(filterVaiant(selected));




答案 3 :(得分:1)

地图索引加入的另一种解决方案:

const selected = [ { "id": 14 }, { "id": 95 } ]


const variants = [
  {
    "id": 1,
    "option_values": [ { "id": 7 }, { "id": 95, } ]
  },
  {
    "id": 2,
    "option_values": [ { "id": 8 }, { "id": 95, } ]
  },
  {
    "id": 3,
    "option_values": [ { "id": 14 }, { "id": 95, } ]
  }
]

function filterVaiant(selected) {
    return variants[variants.map(obj => obj.option_values).map(outterarray => outterarray.map(innerArray=>innerArray.id).join('-') ).indexOf(selected.map(eb=>eb.id).join('-'))];
}

console.log(filterVaiant(selected));