如何找到对象的哪个属性包含特定值?

时间:2018-06-25 18:23:22

标签: javascript arrays json

我需要使用JavaScript遍历对象数组并找到一个特定值,并确定哪个属性是具有该值的属性。例如:

数组:

var objArray = [{
    "ID": 1,
    "Name": "Kathy",
    "Position": "Programer",
    "Hobbies": "Computers",
    "Car": "Mustang"
  },
  {
    "ID": 2,
    "Name": "John",
    "Position": "Programing",
    "Hobbies": "Fishing",
    "Car": "Ferrari"
  },
  {
    "ID": 3,
    "Name": "John",
    "Position": "Sales",
    "Hobbies": "Programing",
    "Car": "Audi"
  },
  {
    "ID": 4,
    "Name": "William",
    "Position": "Marketing",
    "Hobbies": "Movies",
    "Car": "Corvette"
  },
  {
    "ID": 5,
    "Name": "Stephanie",
    "Position": "Director",
    "Hobbies": "Jogging",
    "Car": "Ferrari"
  }
];

假设我要查找包含Program的任何值:

JavaScript:

function findMe(array, value) {
  var results = [];
  for (var i = 0; i < array.length; i++) {
    if (String(Object.values(array[i])).indexOf(value) >= 0) {
      results.push(array[i]);
    }
  }
  return results;
}

var obj = findMe(objArray, 'Program');

到目前为止,只要找到可用的方法,obj就会包含所有包含子字符串Program的条目。

这是我的问题,如何确定数组的哪个属性是包含值的那个?

在这种情况下:有两个Position和一个Hobbies,其中包含值Program

2 个答案:

答案 0 :(得分:2)

首先,通过添加一个在对象的值中找到子字符串的函数来进行一点分解。

接下来,编程问题通常与清楚地描述所需的输出有关。对于建议的规范该如何处理:返回与输入数组相对应的数组。对于输入中的每个对象,返回的数组包含一个键数组,其中给定字符串作为子字符串出现。

// answer the set of keys in object whose string values contain substring
function propsWhoseValueContains(object, substring) {
    return Object.keys(object).filter(key => {
        let value = object[key];
        return (typeof value === 'string') && (value.indexOf(substring) >= 0);
    });
}

// return an array of arrays.  the inner arrays are keys in the source array's
// objects where value appears as a substring of the objects' values

function findMe(array, value) {
    return array.map(object => propsWhoseValueContains(object, value));
}

var objArray = [{
    "ID": 1,
    "Name": "Kathy",
    "Position": "Programer",
    "Hobbies": "Computers",
    "Car": "Mustang"
  },
  {
    "ID": 2,
    "Name": "John",
    "Position": "Programing",
    "Hobbies": "Fishing",
    "Car": "Ferrari"
  },
  {
    "ID": 3,
    "Name": "John",
    "Position": "Sales",
    "Hobbies": "Programing",
    "Car": "Audi"
  },
  {
    "ID": 4,
    "Name": "William",
    "Position": "Marketing",
    "Hobbies": "Movies",
    "Car": "Corvette"
  },
  {
    "ID": 5,
    "Name": "Stephanie",
    "Position": "Director",
    "Hobbies": "Jogging",
    "Car": "Ferrari"
  }
];

console.log(findMe(objArray,"Program"));

答案 1 :(得分:1)

  

这是我的问题,如何确定数组的哪个属性是包含值的那个?

这应该是您想要做的:

var objArray = [{
    "ID": 1,
    "Name": "Kathy",
    "Position": "Programer",
    "Hobbies": "Computers",
    "Car": "Mustang"
},
{
    "ID": 2,
    "Name": "John",
    "Position": "Programing",
    "Hobbies": "Fishing",
    "Car": "Ferrari"
},
{
    "ID": 3,
    "Name": "John",
    "Position": "Sales",
    "Hobbies": "Programing",
    "Car": "Audi"
},
{
    "ID": 4,
    "Name": "William",
    "Position": "Marketing",
    "Hobbies": "Movies",
    "Car": "Corvette"
},
{
    "ID": 5,
    "Name": "Stephanie",
    "Position": "Director",
    "Hobbies": "Jogging",
    "Car": "Ferrari"
}];

var foundObjects = [];

for(var i = 0; i < objArray.length; i++)
    for(var j in objArray[i])
    {
        if((''+objArray[i][j]).indexOf('Program') >= 0)
            foundObjects.push({objIndex: i, propertyIndex: j});
    }


for(i = 0; i < foundObjects.length; i++)
{
    var obj = foundObjects[i];
    console.log('Word "Program" was found by object with index: ' + obj.objIndex + ' in the property: ' + obj.propertyIndex);

}