基于另一个数组从对象数组中过滤数组属性

我想根据另一个数组过滤具有数组属性的对象数组:


[

  {

   id: 50,

   name: 'test1',

   countries: ['RO','GB'],

  }, {

    id: 51,

    name: 'test2',

    countries: ['DE', 'RO'],

  }, {

    id: 52,

    name: 'test3',

    countries: ['DE'],

  }

]

我想返回一个对象数组,这些对象可以通过一组国家/地区进行过滤,这意味着如果我想通过 1、2 个国家/地区进行过滤。


1. countries: ['RO']

或者


2. countries: ['RO', 'DE'],

预期输出将是:


1.


[

  {

   id: 50,

   name: 'test1',

   countries: ['RO', 'DE' ,'GB'],

  }, {

    id: 51,

    name: 'test2',

    countries: ['DE', 'RO'],

  }

]

2.


[

  {

   id: 50,

   name: 'test1',

   countries: ['RO', 'DE' ,'GB'],

  }, {

    id: 51,

    name: 'test2',

    countries: ['DE', 'RO'],

  }, {

    id: 52,

    name: 'test3',

    countries: ['DE'],

  }

]


吃鸡游戏
浏览 163回答 2
2回答

慕容708150

您可以使用filter()和includes()方法;var import1 = [  {   id: 50,   name: 'test1',   countries: ['RO','GB'],  }, {    id: 51,    name: 'test2',    countries: ['DE', 'RO'],  }, {    id: 52,    name: 'test3',    countries: ['DE'],  }];var export1 =  import1.filter(function(importz){    return importz.countries.includes("DE");});console.log(export1);

慕娘9325324

使用此函数查找javascript数组中存在的多个值var containsAll = arr1.every(function (i) { return arr2.includes(i); });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript