
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Find specific key value in array of objects using JavaScript
Suppose we have a JSON object like this −
const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" }], "MOUSE": [{ "productId": "789" }, { "productId": "012" }], "KEY-BOARD": [{ "productId": "345" }] };
We are required to write a JavaScript function that takes in one such object as the first argument, and a key value pair as the second argument.
The key value pair is basically nothing but an object like this −
const pair = {"productId": 456};
The function should then search the object for the key with specified "productId" and return that.
Example
The code for this will be −
const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" }], "MOUSE": [{ "productId": "789" }, { "productId": "012" }], "KEY-BOARD": [{ "productId": "345" }] }; const searchByPair = (obj = {}, pair = {}) => { const toSearch = Object.values(pair)[0]; let required = undefined; Object.keys(obj).forEach((key) => { if(obj[key].find((pid) => pid.productId === toSearch)){ required = key; } }); return required; }; console.log(searchByPair(obj, { 'productId': '123' }));
Output
And the output in the console will be −
LAPTOP
- Related Articles
- Find MongoDB documents where all objects in array have specific value?
- Fetch specific values from array of objects in JavaScript?
- Merge objects in array with similar key JavaScript
- Filter array of objects by a specific property in JavaScript?
- Get only specific values in an array of objects in JavaScript?
- How to group an array of objects by key in JavaScript
- How to create an array with multiple objects having multiple nested key-value pairs in JavaScript?
- How to Sort object of objects by its key value JavaScript
- Using methods of array on array of JavaScript objects?
- Sort array of objects by string property value in JavaScript
- Retrieve property value selectively from array of objects in JavaScript
- Sort array of objects by string property value - JavaScript
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- Get max value per key in a JavaScript array
- How to store a key => value array in JavaScript?

Advertisements