【问题标题】:How can i check for a specific object (data) in an array?如何检查数组中的特定对象(数据)?
【发布时间】:2019-05-24 16:23:52
【问题描述】:

在编程方面我是个初学者,目前遇到了问题。我有一个包含 4 个项目的数组(您可以在下面的代码部分看到数组),所有四个项目都有其特定的 id (1-4)。

我想要编程的东西是一种为每个数组项运行单独代码的方法。我想我可以通过制作 if 语句来解决这个问题,我只需检查 id (每个项目都是单独的)。但是我该怎么做呢??

如果有人有更好的主意,他可以肯定地告诉我那个 id。

最好的问候约翰。

{ id: 1, name: 'BMW', price: 250, quantity: ''},

{ id: 2, name: 'Google', price: 110, quantity: ''},

{ id: 3, name: 'Apple', price: 1000, quantity: ''},

{ id: 4, name: 'Twitter', price: 50, quantity: ''}

【问题讨论】:

标签: javascript html css vue.js vuejs2


【解决方案1】:

您可以使用 if 语句来实现这一点。对此有不同的方法,但基本方法将保持不变。循环遍历数组中的每个元素,然后检查 id。您可以使用传统的 for 循环,也可以使用 somefilter 等方法。

【讨论】:

    【解决方案2】:

    您可以使用简单的 for 循环简单地迭代您的数组并检查 id 是否等于给定的 id 然后返回整个对象。如果 id 不匹配,它将返回undefined。考虑以下代码 sn-p:

    let array = [{ id: 1, name: 'BMW', price: 250, quantity: ''},
    
    { id: 2, name: 'Google', price: 110, quantity: ''},
    
    { id: 3, name: 'Apple', price: 1000, quantity: ''},
    
    { id: 4, name: 'Twitter', price: 50, quantity: ''}];
    
    function getValue(id)
    {
      for( var index=0;index<array.length;index++){
         if( array[index].id === id){
            return array[index];
         }
      };
    }
    
    console.log(getValue(1));
    console.log(getValue(5));

    【讨论】:

    • @JohnKohlmeier 很乐意为您提供帮助。你可以接受我的回答。
    【解决方案3】:

    据我了解,您希望为数组中的不同项目运行单独的代码段。如果数组的元素是固定的,即它们不会改变,那么您可以为每个项目编写不同的代码作为函数并将其作为属性添加到相应的项目。

    请看下面的例子:

    let BMWFunction = function(){
       console.log('BMW!');
    }
    
    let googleFunction = function(){
       console.log('Google!');
    }
    
    let myArray = [
       { id: 1, name: 'BMW', price: 250, quantity: '', code: BMWFunction},
       { id: 2, name: 'Google', price: 110, quantity: '', code: googleFunction }
    ]
    
    for (let i = 0; i < myArray.length; i++){
       myArray[i].code();
    }
    

    那么对于你在数组中循环遍历的每一项,你可以调用相关的代码属性。

    【讨论】:

      【解决方案4】:

      使用 filter() 循环并比较每个项目的 id 与所需文本 - 请注意,然后我可以返回关联的对象(例如 - 如果你想显示名称) - 或者一个文本字符串,如果它不存在。

      您还可以在其中添加逻辑以确保 id 是唯一的 - 即,如果为给定的 n id 找到多个结果 - 那么您需要编辑重复项的 id。

      let items = [
        { id: 1, name: 'BMW', price: 250, quantity: ''},
        { id: 2, name: 'Google', price: 110, quantity: ''},
        { id: 3, name: 'Apple', price: 1000, quantity: ''},
        { id: 4, name: 'Twitter', price: 50, quantity: ''}
      ]
      
      function findItem(id){
        
        var foundItem = items.filter(function(item){
          return(item.id == id) 
         });
         
         if(foundItem.length > 0) {
          return foundItem[0];
         } else {
          return "Item not found";
         }
      }
      
      console.log(findItem('1')); // returns the object of the BMW
      console.log(findItem('6')); // returns "Item not found"

      【讨论】:

        猜你喜欢
        • 2013-09-13
        • 1970-01-01
        • 2022-01-22
        • 2018-01-03
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        • 2013-05-06
        • 2019-02-16
        相关资源
        最近更新 更多