Research and Practice of JavaScript Object Value Search Algorithms

Nov 21, 2025 · Programming · 11 views · 7.8

Keywords: JavaScript | Object Search | Array Filtering | Object.values | Functional Programming

Abstract: This paper provides an in-depth exploration of various methods for searching object array values in JavaScript. By analyzing the differences between traditional for loops and modern functional programming, it details implementation solutions using core APIs such as indexOf, includes, Object.keys, and Object.values. The article includes complete code examples, performance comparisons, and best practice recommendations to help developers master efficient object search techniques.

Introduction

In modern web development, processing object arrays and performing value-based searches are common requirements. This paper systematically studies multiple implementation methods for object value search in JavaScript based on practical development scenarios.

Problem Definition and Requirements Analysis

Given a homogeneous object array, it is necessary to search for all objects that contain a specific keyword in their values (not keys). For example, for the array:

[
  {
    "foo" : "bar",
    "bar" : "sit"
  },
  {
    "foo" : "lorem",
    "bar" : "ipsum"
  },
  {
    "foo" : "dolor",
    "bar" : "amet"
  }
]

Searching for the keyword "r" should return all three objects, as each object contains the character "r" in one of its values.

Traditional Implementation Methods

Using nested loops is the most intuitive solution. The outer loop iterates through each object in the array, while the inner loop iterates through each property value of the object.

function searchObjects(objects, searchTerm) {
  const results = [];
  
  for (let i = 0; i < objects.length; i++) {
    const obj = objects[i];
    let found = false;
    
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (obj[key].indexOf(searchTerm) !== -1) {
          found = true;
          break;
        }
      }
    }
    
    if (found) {
      results.push(obj);
    }
  }
  
  return results;
}

Enhanced Function Implementation

In practical applications, enhanced features such as string trimming and result deduplication need to be considered.

function trimString(str) {
  return str.trim();
}

function isObjectEqual(obj1, obj2) {
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);
  
  if (keys1.length !== keys2.length) return false;
  
  for (const key of keys1) {
    if (obj1[key] !== obj2[key]) return false;
  }
  
  return true;
}

function containsObject(arr, obj) {
  return arr.some(item => isObjectEqual(item, obj));
}

function advancedSearch(objects, searchTerm) {
  const trimmedTerm = trimString(searchTerm);
  const results = [];
  
  for (const obj of objects) {
    let matchFound = false;
    
    for (const key in obj) {
      if (obj.hasOwnProperty(key) && 
          typeof obj[key] === 'string' && 
          obj[key].indexOf(trimmedTerm) !== -1) {
        matchFound = true;
        break;
      }
    }
    
    if (matchFound && !containsObject(results, obj)) {
      results.push(obj);
    }
  }
  
  return results;
}

Modern Functional Implementation

ES6 introduced more concise functional programming methods. Using Object.keys and array methods enables writing more elegant code.

function functionalSearch(objects, searchTerm) {
  return objects.filter(obj => 
    Object.keys(obj).some(key => 
      typeof obj[key] === 'string' && 
      obj[key].includes(searchTerm)
    )
  );
}

Optimized Solution Based on Object.values

The Object.values() method can directly obtain all values of an object, simplifying the search logic.

function valuesBasedSearch(objects, searchTerm) {
  return objects.filter(obj => 
    Object.values(obj).some(value => 
      typeof value === 'string' && 
      value.includes(searchTerm)
    )
  );
}

Performance Analysis and Comparison

Different implementation methods vary in performance:

Edge Case Handling

Various edge cases need to be considered in practical applications:

function robustSearch(objects, searchTerm) {
  if (!Array.isArray(objects) || !searchTerm) {
    return [];
  }
  
  const term = String(searchTerm).trim();
  if (!term) return [];
  
  return objects.filter(obj => {
    if (obj && typeof obj === 'object') {
      return Object.values(obj).some(value => 
        value != null && 
        String(value).includes(term)
      );
    }
    return false;
  });
}

Practical Application Scenarios

This technology can be applied to:

Conclusion and Best Practices

JavaScript object value search has multiple implementation methods. The choice depends on specific requirements: choose traditional loops for performance priority, and functional methods for code conciseness. It is recommended to conduct performance testing in actual projects to select the most suitable solution.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.