0

There are 2 arrays tooarray and moarray what I want is list of all elements which are in moarray but not in 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;
            })
    });

output recieving

  tInsertArray []

output expected

tInsertArray [wedwewdwe]
Rohan Singh
  • 71
  • 1
  • 1
  • 8

5 Answers5

2

Filter all elements from moarray where there is no element in tooarray that has name property equal to each moarray entry:

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)
connexo
  • 41,035
  • 12
  • 60
  • 87
1

You can use filter and find

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.find(e => e.name === x));

console.log( tInsertArray );
Eddie
  • 25,279
  • 6
  • 26
  • 53
1

Grab the names and then do the filter

var tInsertArray = moarray.filter(x => {
    return tooarray.map(y => y.name).includes(x) === false;
});
Meghan
  • 1,083
  • 11
  • 16
1

You have no return in first callback. That's why array comes empty.

Ilya Novojilov
  • 851
  • 6
  • 12
0

You could filter with a check if the name exists.

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' }],
    moarray = ['wedwewdwe', 'Other', 'Tincture', 'Cannabis'],
    tInsertArray = moarray.filter(x => !tooarray.some(({ name }) => name === x));

console.log(tInsertArray);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324