Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all matching elements with in an array of objects [duplicate]

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?

like image 588
ankur Avatar asked Sep 13 '18 09:09

ankur


People also ask

How do you find a matching element in an array?

To find the first array element that matches a condition:Use the Array. find() method to iterate over the array. Check if each value matches the condition. The find method returns the first array element that satisfies the condition.

How do you find a repeated value in an array?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }

How do I find all the values in an array?

To check if all of the values in an array are equal to true , use the every() method to iterate over the array and compare each value to true , e.g. arr. every(value => value === true) . The every method will return true if the condition is met for all array elements. Copied!


1 Answers

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);
like image 177
Orelsanpls Avatar answered Oct 08 '22 15:10

Orelsanpls