简体   繁体   English

如何在对象数组中搜索特定匹配的属性值?

[英]How to search for a particular matching property value inside an array of objects?

I want to search only in name property of the list of json objects.我只想在 json 对象列表的name属性中搜索。 This is the json array -这是 json 阵列 -

combinedArr = [
  {
    lat: "36.1152",
    locality: "Worth Avenue, Unit A",
    long: "117.521",
    name: "Bazaar Cafe",
    postal_code: "20619",
    street_no: "45250",
  },
  {
    lat: "31.21",
    locality: "Ambey Mantion",
    long: "119.7",
    name: "Ashley's Cafe",
    postal_code: "29087",
    street_no: "1B",
  },
];

This is the search function, this search function search in all properties of the objects but I just want it to search only in name property and return list of search results.这是搜索 function,此搜索 function 搜索对象的所有属性,但我只希望它仅在name属性中搜索并返回搜索结果列表。 The search function is the following -搜索 function 如下——

  function findCaliforniaCafes(searchTerm) {
    const filterByValue = (combinedArr, string) => {
      return combinedArr.filter((o) =>
        Object.keys(o).some((k) =>
          o[k].toLowerCase().includes(string.toLowerCase())
        )
      );
    };

    const result = searchTerm
      ? filterByValue(combinedArr, searchTerm)
      : combinedArr;

    return result;
  }

Just use filter like so:只需像这样使用filter

const filtered = combinedArr.filter(({ name }) => name.includes("Ashley"));

 const combinedArr = [{lat:"36.1152",locality:"Worth Avenue, Unit A",long:"117.521",name:"Bazaar Cafe",postal_code:"20619",street_no:"45250",},{lat:"31.21",locality:"Ambey Mantion",long:"119.7",name:"Ashley's Cafe",postal_code:"29087",street_no:"1B",}]; const filtered = combinedArr.filter(({ name }) => name.toLowerCase().includes(searchTerm)); console.log(filtered);
 .as-console-wrapper { max-height: 100%;important: top; auto; }

You can use array filter() .您可以使用数组filter()

This will return an array containing all elements that pass the condition.这将返回一个包含所有通过条件的元素的数组。

const searchFilter = "Bazaar Cafe"


const filtered = combinedArr.filter(item => item.name === searchFilter);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从对象数组内的对象数组中搜索值? - how to search a value from a array of objects inside array of objects? 在对象数组中搜索匹配的属性和值对 - Searching for matching property and value pairs in an array of objects Javascript:如何为对象数组中的 object 属性设置值? - Javascript: How to set value to an object property inside an array of objects? 如何在 JS 中将一个对象数组的属性值添加到另一个对象数组(用于匹配对象) - How to add property value from one array of objects into another (for the matching objects) in JS Vue.js-如何通过特定属性对数组内的对象进行排序和使用“ v-for”呈现 - Vue.js - how to sort and objects inside the array by particular property and render it with “v-for” 获取对象数组内对象属性的值 - Get value of object property inside array of objects 如何在 javascript 中的未知深度对象数组中找到特定属性值的最大深度? - How do I find the maximum depth of a particular property value in an array of objects of unknown depth in javascript? 快速搜索对象数组以查找匹配的键/值对的方法 - Fast way to search an array of objects for a matching key/value pair 如何搜索数组并返回匹配值 - how to search through the array and return the matching value JavaScript快捷方式,用于检查对象数组中的特定键值 - JavaScript shortcut for checking a particular key-value inside array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM