我要2个数组tooarraymoarraymoarray而不是tooarray的所有元素的列表。

var tooarray = [
  { catalogue_id: 23480,
    parent_category_id: 23479,
    user_id: 28434,
    form_id: 4261,
    name: 'Other'
  },
{ catalogue_id: 23481,
  parent_category_id: 23479,
  user_id: 28434,
  form_id: 4261,
  name: 'Tincture',
  description: 'Tincture'
},
{ catalogue_id: 23482,
  parent_category_id: 23479,
  user_id: 28434,
  form_id: 4261,
  name: 'Cannabis',
  description: 'Cannabis'
 },
 ]


var moarray = [
  'wedwewdwe',
  'Other',
  'Tincture',
  'Cannabis'
  ];

 var tInsertArray = moarray.filter(x =>{
            tooarray.filter(y=>{
              return x.indexOf(y.name) < 0;
            })
    });


输出接收

  tInsertArray []


预期输出

tInsertArray [wedwewdwe]

最佳答案

moarray过滤所有元素,其中tooarray中没有元素的name属性等于每个moarray条目:



let tooarray = [{
    catalogue_id: 23480,
    parent_category_id: 23479,
    user_id: 28434,
    form_id: 4261,
    name: 'Other'
  },
  {
    catalogue_id: 23481,
    parent_category_id: 23479,
    user_id: 28434,
    form_id: 4261,
    name: 'Tincture',
    description: 'Tincture'
  },
  {
    catalogue_id: 23482,
    parent_category_id: 23479,
    user_id: 28434,
    form_id: 4261,
    name: 'Cannabis',
    description: 'Cannabis'
  },
]

let moarray = [
  'wedwewdwe',
  'Other',
  'Tincture',
  'Cannabis'
];

let tInsertArray = moarray.filter(x => !tooarray.filter(y => y.name === x).length)

console.log(tInsertArray)

关于javascript - 过滤2个数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49221006/

10-11 04:06