Reputation: 91
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]
Upvotes: 0
Views: 58
Reputation: 56773
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)
Upvotes: 2
Reputation: 386680
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);
Upvotes: 0
Reputation: 889
You have no return
in first callback. That's why array comes empty.
Upvotes: 1
Reputation: 1261
Grab the names and then do the filter
var tInsertArray = moarray.filter(x => {
return tooarray.map(y => y.name).includes(x) === false;
});
Upvotes: 1
Reputation: 26844
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 );
Upvotes: 1