【问题标题】:Find a property (which is stored as a string in a variable) in an array of objects在对象数组中查找属性(作为字符串存储在变量中)
【发布时间】:2019-02-20 19:42:38
【问题描述】:

我有一个存储不同“学生”数据的对象数组。

var pupils = [
{
    id: 0,
    name: 'will'
},
{
    id: 1,
    name: 'megan'
}
];

我想创建一个名为“findPupil”的函数,它接受三个参数:您知道的属性、您知道的值以及您要查找的属性。

假设您知道要查找的学生的 id 为 1,但您不知道他们的名字。在这种情况下,您可以这样调用函数:

var name = findPupil('id', 1, 'name'); // should now store 'Megan' as a string

这是我写的函数:

function findPupil(property, value, find) {
pupils.forEach(function(pupil) {
    if(pupils[pupil][`${property}`] === value) {
      return pupils[pupil][`${find}`];
    }
  });
}

调用此函数返回以下内容:

Error: Cannot read property 'id' of undefined

如何让这个功能发挥作用?

【问题讨论】:

  • 什么是array1
  • 抱歉,复制粘贴错误

标签: javascript arrays foreach javascript-objects


【解决方案1】:

使用Array.find() 查找具有您知道的属性的对象,如果找不到对象,则使用默认值。从对象中提取find 属性:

const pupils = [{"id":0,"name":"will"},{"id":1,"name":"megan"}];

const findPupil = (property, value, find) =>
  (pupils.find(o => o[property] === value) || {})[find];

const name = findPupil('id', 1, 'name');

console.log(name);

【讨论】:

    【解决方案2】:

    你可以使用.find()

    var pupils = [{id: 0, name: 'will'},{id: 1,name: 'megan'}];
    
    function getByPropertyAndValue(knownProp, knownValue, desiredProperty) {
      let pupil = pupils.find(p => p[knownProp] === knownValue); //Find pupil by property and value
      return pupil && pupil[desiredProperty]; //Return the value, or undefined if not found
    }
    
    console.log(getByPropertyAndValue("id", 1, "name"));

    【讨论】:

      【解决方案3】:

      除了if 语句的一部分之外,您的大部分代码都是正确的。请找到工作示例 -

      var pupils = [{
          id: 0,
          name: 'will'
        },
        {
          id: 1,
          name: 'megan'
        }
      ];
      
      function findPupil(property, value, find) {
        let result
        pupils.forEach(function(pupil) {
          if (pupil[property] === value) {
            result = pupil[find];
          }
        });
        return result
      }
      
      var name = findPupil('id', 1, 'name');
      
      console.log(name)

      【讨论】:

      • `${property}` 此处不需要模板文字,因为 propertyfind 已经是字符串。
      【解决方案4】:
          var pupils = [
          { id: 0, name: 'will' },
          { id: 1, name: 'megan' }
      ];
      
      function findPupil(property, value, find) {
          var find_value = '';
          for (var i = 0; i < pupils.length; i++) {
              if (find_value == '') {
                  Object.keys(pupils[i]).forEach(function (key) {
                      if (key == property && pupils[i][key] == value) {
                          find_value = pupils[i][find];
                      }
                  });
              } else {
                  break;
              }
          }
          console.log(find_value);
          return find_value;
      }
      
      var name = findPupil('id', 1, 'name');
      

      【讨论】:

        猜你喜欢
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-05
        • 2017-03-01
        • 1970-01-01
        相关资源
        最近更新 更多