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.split(',').indexOf('4') !== -1);

console.log(items);

Answer from Orelsanpls on Stack Overflow
Discussions

javascript - How to get list of all objects in an array of objects? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Closed 5 years ago. I have an array of object such that every element has unique objects. How can I make a list of all ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Return a property from all matching elements in an array of objects? - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Domain expertise still wanted: the latest trends in AI-assisted knowledge for... ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... ... 0 How do I return an array of objects that only contains a certain object in an array for Javascript... More on stackoverflow.com
🌐 stackoverflow.com
March 10, 2016
How to find object in array of objects, who's nested array matches all the objects in a separate array?
You're going to have to clarify better what it is you're wanting to achieve. Must the selectedOptions property of a productVariant only include the options (and can have more) or must they be exactly the same? But at first glance, would I would say is, when you provided the example of the data, you have probably incorrectly used String to declare the value of the property optionalFields. This is expecting a value and not a type annotation! Remember the property is optional, so if there is no value, the entire property can be omitted. The easiest way to compare two objects is probably to stringify them both then check their equality. Here is a quick attempt, with some assumptions made (that may be incorrect!) and changes made for readability: type Option = { name: string; value: string; optionalFields?: string }; const options: Option[] = [ { name: "Format", value: "Hardback" }, { name: "Colour", value: "Pink" }, { name: "Size", value: "Large" }, ]; const productVariants: Option[][] = [ // Not filtered [ { name: "Format", value: "Zine" }, { name: "Colour", value: "Grey" }, ], // This would be filtered (as it matches all the options) [ { name: "Format", value: "Hardback" }, { name: "Colour", value: "Pink" }, { name: "Size", value: "Large" }, { name: "adidtionalProperty", value: "test" }, ], ]; const result = productVariants.filter((productVariant) => options.every((option) => productVariant.some((x) => x.name === option.name && x.value === option.value)) ); console.log(result); More on reddit.com
🌐 r/learnjavascript
4
1
December 21, 2022
Trying to diff array of objects with Lodash in Angular

You want an array C which contains all the objects from array A which weren't in array B? And strict equality won't work?

C = A - B:

let a = [{x: 0}, {x: 1}, {x: 2}, {x: 3}];
let b = [{x: 1}, {x: 2}];
let c = a.filter(item => !b.some(other => item.x === other.x));
console.log(c); // [{x:0}, {x: 3}]

Something like that, I guess.

By the way, next time, put a test case on JSFiddle, JSBin, or CodePen. Also, remove all unnecessary details like that it's about users and security and whatever. Remove everything which isn't relevant.

More on reddit.com
🌐 r/javascript
4
1
December 26, 2015
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › find
Array.prototype.find() - JavaScript - MDN Web Docs
It calls a provided callbackFn function once for each element in an array in ascending-index order, until callbackFn returns a truthy value. find() then returns that element and stops iterating through the array. If callbackFn never returns a truthy value, find() returns undefined. Read the iterative methods section for more information about how these methods work in general. callbackFn is invoked for every index of the array, not just those with assigned values.
🌐
Zipy
zipy.ai › blog › find-object-by-property-in-javascript-array
find object by property in javascript array
April 12, 2024 - We then call this function with the books array, passing 'genre' as the property and 'Fiction' as the value, to find all books with the genre 'Fiction'. Debug and fix code errors with Zipy Error Monitoring. ... Finding objects by property in JavaScript arrays is a common task in web development.
🌐
Milddev
milddev.com › how-to-find-items-in-array-of-objects-in-javascript
Find Items in Array of Objects JS
Search JavaScript arrays of objects using find(), filter(), loops, and best practices to locate and return desired items efficiently.
🌐
Ubiq BI
ubiq.co › home › how to search for object in array of objects in javascript
How to Search For Object in Array of Objects in JavaScript - Ubiq BI
March 6, 2025 - If you only want to look for first occurrence of an object, use find() function. If you want an array of all matching objects, use filter() function.
🌐
Stack Abuse
stackabuse.com › bytes › find-an-object-by-id-in-an-array-of-javascript-objects
Find an Object by ID in an Array of JavaScript Objects
September 20, 2023 - In the next few sections we'll see a few different ways in which you can find an object by ID in an array of objects. The find method is a built-in function in JavaScript that can be used to locate an object by its ID (or other property) in an array of objects.
Find elsewhere
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-find-an-object-by-property-value-in-an-array-of-javascript-objects.php
How to Find an Object by Property Value in an Array of JavaScript Objects
You can simply use the find() method to find an object by a property value in an array of objects in JavaScript. The find() method returns the first element in the given array that satisfies the provided testing function.
🌐
CodingDeft
codingdeft.com › posts › js-find-object-in-array-of-objects
How to find an object in an Array of objects in JavaScript | CodingDeft.com
Tutorial on how to find an object in an Array of objects in JavaScript using find, findIndex, using unique key etc
🌐
W3Schools
w3schools.com › jsref › jsref_find.asp
JavaScript Array find() Method
Dot [ ] Bracket [ ] Array Literal { } Block { } Object Literal ?. Chaining ... Spread ( ) Grouping ( ) ? x : y Ternary ( ) Invocation => Arrow delete in instanceof typeof void yield yield* JS Precedence ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS BigInt
🌐
Medium
medium.com › @lama.ibrahim96 › 5-ways-to-find-a-value-in-an-array-of-objects-in-javascript-130fa4856e53
5 ways to find a value in an array of objects in Javascript | by Lama Ibrahim | Medium
April 5, 2023 - function findUser() { return users.find((user) => !user.isActive) } console.log(findUser()) //{ id: 2, name: 'Mike', isActive: false } return A shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned. Array.filter() returns an array of all the objects that pass the test implemented by the provided function, if we want to return only the first user we can return index 0 of the returned array.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-array-search-methods
Four Methods to Search Through Arrays in JavaScript | DigitalOcean
December 15, 2021 - Prior to ECMAScript 6, you probably would have used a for loop to iterate through all the items in the array and perform operations on each item. Now there are several built-in utility methods that solve some of the common tasks for searching for values in an array. In this article, you will learn about Array.includes(), Array.indexOf, Array.find(), and Array.filter. If you would like to follow along with this article, you will need: Familiarity with JavaScript arrays.
🌐
Sentry
sentry.io › sentry answers › javascript › find object by property in javascript array
Find object by property in JavaScript array | Sentry
In this code, we’ve used JavaScript’s arrow function expression syntax to keep things concise. If we would like to get the array index of the object instead of the object itself, we can use findIndex instead: const objectList = [ { id: 10, name: "Jane" }, { id: 36, name: "Steven" } ]; function findNameById(list, id) { return list[list.findIndex((obj) => obj.id === id)].name; } console.log(findNameById(objectList, 36)); // will print "Steven" If we need to get all elements that satisfy the condition rather than just the first one, we should use the filter method, which returns an array of matched objects.
🌐
Mimo
mimo.org › glossary › javascript › array-find
JavaScript Array() Find: Syntax, Usage, and Examples
You can easily search for strings that meet specific conditions using the array find JavaScript method. A common use case for find() is searching for objects in arrays of objects.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-access-properties-from-an-array-of-objects-in-javascript
How to Access Properties from an Array of Objects in JavaScript
February 29, 2024 - If we are looking for a specific object from an array of objects, we can use the find method. The find method returns the first element in the array that satisfies the provided testing function.
🌐
SheCodes
shecodes.io › athena › 12988-how-to-access-array-of-objects-properties-in-javascript
[JavaScript] - How to access array of objects properties in | SheCodes
Learn how to access the properties inside an array of objects in JavaScript and perform operations using if else statements.
🌐
Infragistics
infragistics.com › blog › how to › how to locate a particular object in a javascript array
How To Locate a Particular Object in a JavaScript Array
February 18, 2025 - The JavaScript find method always executes the callback function with three arguments: element, index, array. Let us see some examples of using the find method!
🌐
Reddit
reddit.com › r/learnjavascript › how to find object in array of objects, who's nested array matches all the objects in a separate array?
r/learnjavascript on Reddit: How to find object in array of objects, who's nested array matches all the objects in a separate array?
December 21, 2022 -

I have an array of options and an array of productVariants. I would like to find the productVariant who's selectedOptions property matches all the name and value fields in an option array?

Types would look like this

const options: Array<{name: String, value: String}>   const productVariants: Array<{selectedOptions: Array<{name: String, value: String, optionalFields?: String}>}> 

Example of the data here:

const options = [{ name: “Format”, value: “Hardback” }, { name: “Colour”, value: “Pink” }, { name: “Size”, value: “Large” }]  const productVariants = [{selectedOptions: [{name: "Format", value: "Zine", optionalFields: “abc”},{name: "Colour", value: "Grey", optionalFields: “abc”}]}, {selectedOptions: [{name: “Format”, value: "Zine", optionalFields: “abc”},{name: “Colour”, value: “blue”, optionalFields: “abc”}]}] 

My desired output would be to retrive a single productVariant object who's selectedOptions array matches the options Array.

Thanks for the help!

Top answer
1 of 1
2
You're going to have to clarify better what it is you're wanting to achieve. Must the selectedOptions property of a productVariant only include the options (and can have more) or must they be exactly the same? But at first glance, would I would say is, when you provided the example of the data, you have probably incorrectly used String to declare the value of the property optionalFields. This is expecting a value and not a type annotation! Remember the property is optional, so if there is no value, the entire property can be omitted. The easiest way to compare two objects is probably to stringify them both then check their equality. Here is a quick attempt, with some assumptions made (that may be incorrect!) and changes made for readability: type Option = { name: string; value: string; optionalFields?: string }; const options: Option[] = [ { name: "Format", value: "Hardback" }, { name: "Colour", value: "Pink" }, { name: "Size", value: "Large" }, ]; const productVariants: Option[][] = [ // Not filtered [ { name: "Format", value: "Zine" }, { name: "Colour", value: "Grey" }, ], // This would be filtered (as it matches all the options) [ { name: "Format", value: "Hardback" }, { name: "Colour", value: "Pink" }, { name: "Size", value: "Large" }, { name: "adidtionalProperty", value: "test" }, ], ]; const result = productVariants.filter((productVariant) => options.every((option) => productVariant.some((x) => x.name === option.name && x.value === option.value)) ); console.log(result);