66

I have an array of objects

I am searching within the array like this

let arr = [
    { name:"string 1", arrayWithvalue:"1,2", other: "that" },
    { name:"string 2", arrayWithvalue:"2", other: "that" },
    { name:"string 2", arrayWithvalue:"2,3", other: "that" },
    { name:"string 2", arrayWithvalue:"4,5", other: "that" },
    { name:"string 2", arrayWithvalue:"4", other: "that" },
];
var item  = arr.find(item => item.arrayWithvalue === '4'); 
console.log(item)

This should return an array with this two rows

{ name:"string 2", arrayWithvalue:"4,5", other: "that" },
{ name:"string 2", arrayWithvalue:"4", other: "that" }

It returns only one row which is the first match.

{ name:"string 2", arrayWithvalue:"4", other: "that" }

I do not want to use any external libraries for this. How can I return all the matches that match the criteria?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
ankur
  • 4,565
  • 14
  • 64
  • 100

6 Answers6

103

Two things: first, Array.find() returns the first matching element, undefined if it finds nothing. Array.filter returns a new array containing all matching elements, [] if it matches nothing.

Second thing, if you want to match 4,5, you have to look into the string instead of making a strict comparison. To make that happen we use indexOf which is returning the position of the matching string, or -1 if it matches nothing.


Example:

const arr = [
  {
    name: 'string 1',
    arrayWithvalue: '1,2',
    other: 'that',
  },
  {
    name: 'string 2',
    arrayWithvalue: '2',
    other: 'that',
  },
  {
    name: 'string 2',
    arrayWithvalue: '2,3',
    other: 'that',
  },
  {
    name: 'string 2',
    arrayWithvalue: '4,5',
    other: 'that',
  },
  {
    name: 'string 2',
    arrayWithvalue: '4',
    other: 'that',
  },
];

const items = arr.filter(item => item.arrayWithvalue.indexOf('4') !== -1);

console.log(items);
Josselin
  • 2,593
  • 2
  • 22
  • 35
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
10

Use array filter method. Like

arr.filter(res => res.arrayWithvalue.indexOf('4') !== -1);
Eklavya
  • 17,618
  • 4
  • 28
  • 57
3

You need to use the filter method in place of find. This will return a new array containing just the members that return a truthy value from the passed in function.

2

Array.prototype.find() will, as per the MDN spec: return the value of the first element in the array that satisfies the provided testing function.

What you want to use instead is the filter function .filter() which will return an array of all instances that match your testing function.

Adam
  • 1,724
  • 13
  • 16
-3

With filter and charAt.

const result = arr.filter(item => item.arrayWithvalue.charAt(0) === '4');
Kishlin
  • 373
  • 2
  • 15
  • 2
    So know what happen if you wanna match 42 number? It's a bad advise to use `item.arrayWithvalue.charAt(0)` – Orelsanpls Sep 13 '18 at 10:02
-3

Use array.filter:

var arr = [
    { name:"string 1", arrayWithvalue:"1,2", other: "that" },
    { name:"string 2", arrayWithvalue:"2", other: "that" },
{ name:"string 2", arrayWithvalue:"2,3", other: "that" },
{ name:"string 2", arrayWithvalue:"4,5", other: "that" },
{ name:"string 2", arrayWithvalue:"4", other: "that" },
];

var res = arr.filter(e => e.arrayWithvalue.split(',')[0] === '4');
console.log(res);
Faly
  • 13,291
  • 2
  • 19
  • 37
  • So know what happen if you wanna match `42` number? It's a bad advise to use `e.arrayWithvalue.split(',')[0]` – Orelsanpls Sep 13 '18 at 10:01