简体   繁体   English

如何将输入与一个 object 键和值从具有更多对象和更多键和值的数组中进行比较

[英]How to compare an input with one object key and value from an array with more objects with more key and values

I am new to coding (one month) and in here and I am a little bit stuck.我是编码新手(一个月),在这里我有点卡住了。 Creating an autocomplete and i am trying to compare an input with one object key and value from an array with more objects with more key and values.创建一个自动完成,我试图将输入与一个 object 键和值从一个具有更多对象的数组中进行比较,该数组具有更多键和值。

This is what I have so far.这就是我到目前为止所拥有的。 but I am a little bit stuck.但我有点卡住了。

ingredients is the array with more objects where I keep the info成分是包含更多对象的数组,我在其中保存信息

$("#ingredientDetails").click(function () {
  var searchQuery = $("input[type = 'text']").val();
  for (var i = 0; i > ingredients.length; i++) {
    if (searchQuery === Object.name(ingredients)) {
      console.log("matching");
    } else {
      console.log("not matching");
    }
  }
});

The loop you are using will not run if you have one or more items in ingredients array as you have used i > ingredient.length in your for loop.如果您在 for 循环中使用了i > ingredient.length在成分数组中有一个或多个项目,则您正在使用的循环将不会运行。 Try the following尝试以下

for (var i = 0; i < ingredients.length; i++) {
    if (searchQuery === Object.name(ingredients)) {
      console.log("matching");
    } else {
      console.log("not matching");
    }
  }

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

相关问题 如何在已引用一个或多个对象的键中添加其他对象? - how to add additional object to a key that already references one or more objects? 如何使用 Lodash 按多个键分组并将其值汇总到一组对象中? - How to group by more than one key and sum its values in an array of objects, using Lodash? 如何为 Object.assign 中的一个键分配多个值 - How to assign more than one value to a key in Object.assign 比较javascript中的两个或多个对象键值对 - Compare two and more object key value pair in javascript 如何比较一个数组中的值与另一个数组中的键? - How to compare values in one array with key in another? 对于每个 object,如何将特定键值从一个 object 添加到另一个对象数组 - How to add specific key values from one object to another array of objects, for each object 如何根据键和值比较 object 并从数组中删除 - How to compare object based on key and value and remove from array 将对象数组转换为键值对,其中一个值作为键 - Transform an array of objects to key value pairs with one of the values as key 比较对象与数组中的对象,并在键匹配的地方替换值 - Compare object with objects in array and replace value where key match 如何从具有值作为另一个对象数组的 object 中删除键值对? - How to remove key value pair from an object having values as another array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM