-2

I know this has been asked before but I can't seem to find the answer, how to filter multiple data array with valuearray ..?

var array =[
    {
        id : '123',
        nama : 'name one'
    },
    {
        id : '456',
        nama : 'name one'
    },
    {
        id : '789',
        nama : 'name one'
    },
    {
        id : '012',
        nama : 'name one'
    }
]

var valuearray = [123, 456]

array.filter(r => valuearray.includes(r.id)))
Ahmad Sayadi
  • 143
  • 1
  • 2
  • 11
  • 4
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Sep 28 '18 at 08:46
  • `array.filter(i => ~valuearray.indexOf(Number(i.id)))` – Pankaj Parkar Sep 28 '18 at 08:49
  • `array.filter(r => valuearray.includes(Number(r.id)))` – Satpal Sep 28 '18 at 08:49

3 Answers3

3

Your id value in array is string type and values in valuearray is numeric type so you need to use filter() in addition of parseInt() for id values.

var array =[
    {
        id : '123',
        nama : 'name one'
    },
    {
        id : '456',
        nama : 'name one'
    },
    {
        id : '789',
        nama : 'name one'
    },
    {
        id : '012',
        nama : 'name one'
    }
]

var valuearray = [123, 456];
var res = array.filter(i => ~valuearray.indexOf(parseInt(i.id)));
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
2

The way you do it is like this array.filter(element => valuearray.includes(parseInt(element.id))) This will result, following the inputs you have given as example, in the following array: [{ id : '123', nama : 'name one' }, { id : '456', nama : 'name one' }]

I see several answers here using the ~ operator. If you are wondering why that is, it is because indexOf could be 0. If the result of the filtering function is 0, the element will be filtered out, since it is a falsy value. The ~ prevents that.

This should give you more information if you are interested in the ~.


Edit:

As @str mentioned, valuearray has numbers while the id of elements in array are strings. This is why we use parseInt to ensure both terms of the comparison ( the includes function ) are of the same type.

mayk93
  • 1,489
  • 3
  • 17
  • 31
0

The issue is that, in array each object id is string and in the valuearray each one is number.

so we need to convert the number with parseInt method and check.

var array =[
    {
        id : '123',
        nama : 'name one'
    },
    {
        id : '456',
        nama : 'name one'
    },
    {
        id : '789',
        nama : 'name one'
    },
    {
        id : '012',
        nama : 'name one'
    }
]

var valuearray = [123, 456]


var filteredArray = array.filter(o => valuearray.includes(parseInt(o.id)))


console.log(filteredArray)
Learner
  • 8,379
  • 7
  • 44
  • 82