-4

I am having a two array of objects,

let list = [
    {
        "currency": "Albania Lek",
        "abbreviation": "ALL",
    },
    {
        "currency": "Afghanistan Afghani",
        "abbreviation": "AFN",
    },
    {
        "currency": "Argentina Peso",
        "abbreviation": "ARS",
    }];

let c = [
    {
        "value": "AFN"
    },
    {
        "value": "ARS"
    }] 
let asd = list.filter(l=>{
    return l.abbreviation === c.forEach(x=>x.sCurrencyName)
})

i want to return only those object from list which has value in Array of object c

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45

2 Answers2

0

You can use .filter() with .some() method:

let list = [
    {"currency": "Albania Lek", "abbreviation": "ALL"},
    {"currency": "Afghanistan Afghani", "abbreviation": "AFN"},
    {"currency": "Argentina Peso", "abbreviation": "ARS"}
];

let c = [{"value": "AFN"}, {"value": "ARS"}];
    
let result = list.filter(
  ({ abbreviation }) => c.some(({ value }) => value === abbreviation)
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

forEach doesn't return a value that can be used. So when you write l.abbreviation === c.forEach..., there's nothing on the right side of the === sign.

A naive solution would be to use the Array#some function. This function returns a boolean value based on the condition specified, which is what you need here.

So your code becomes

let asd = list.filter(litem => c.some(citem => citem.value === litem.abbreviation))

You can read more about all the different methods available on arrays @ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Akash
  • 5,153
  • 3
  • 26
  • 42