Reputation: 515
This code works for filtering my one Id.
let symbols =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
{symbols:"XEE", id:20},
])
function getStocksOver(stocks, id){
return stocks.filter(function(stock,index){
return stock.id !=id;
});
}
console.log(getStocksOver(symbols,1))
Now i would like to be able to filter by an object
let symbolsDeleted =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
])
Does anyone know a way of doing this cause i really stuck.
Upvotes: 0
Views: 60
Reputation: 4592
I think you can use map and filters
const symbolsDeleted =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
])
let symbols =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
{symbols:"XEE", id:20},
])
function getStocksOver(stocks, oObject) {
const keys = oObject.map(o => o.id);
return stocks.filter((s) => !keys.includes(s.id));
}
console.log(getStocksOver(symbols, symbolsDeleted));
Upvotes: 1
Reputation: 11192
Instead of comparing every stock.id to a single id, you can create an additional filter that filters your symbolsDeleted
array by the stock id.
The length of the filter result will be 0 if there are no matches.
In that case you return true to the outer filter function.
If a match is found, false is returned.
let symbolsDeleted =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
])
let symbols =([
{symbols:"XFX", id:1},
{symbols:"XFF", id:2},
{symbols:"XEE", id:20},
])
function getStocksOver(stocks, objArray){
return stocks.filter(function(stock, index) {
return objArray.filter(function(value) {
return value.id == stock.id
}).length == 0;
});
}
console.log(getStocksOver(symbols, symbolsDeleted))
Upvotes: 1