检查对象是否仅包含一个或两个项目javascript

时间:2015-12-20 09:08:50

标签: javascript

我有一个奇怪的问题需要解决,让我们假设您有一些可以返回的对象示例:

示例1:

{name: "something", age: 17, money: 700}

示例2:

{age: 17, money: 700}

示例3:

{age: 17} // Or
{money: 700}

所以我需要检查一下。如果对象只有agemoneyagemoney

有没有办法使用lodash或纯javascript(更喜欢lodash)来说:你是否包含或包含或包含而没有别的 而没有别的很重要。

4 个答案:

答案 0 :(得分:2)

尝试使用Object.keys()Array.prototype.every()

var obj1 = {
  name: "something",
  age: 17,
  money: 700
};

var obj2 = {
  age: 17,
  money: 700
}

var obj3 = {
    age: 17
  } 

// Or
var obj4 = {
  money: 700
}

var check = function(v) {
  return v === "age" || v === "money"
}

var res1 = Object.keys(obj1).every(check);

var res2 = Object.keys(obj2).every(check);

var res3 = Object.keys(obj3).every(check);

var res4 = Object.keys(obj4).every(check);

console.log(res1, res2, res3, res4)

jsfiddle https://jsfiddle.net/t6hdkats/3

答案 1 :(得分:1)

Javascript不是我最强的技能,所以我确信这可以进行优化。

var a = {name: "something", age: 17, money: 700};

if(Object.keys(a).length == 1 && (a.hasOwnProperty("age") || a.hasOwnProperty("money")) || (Object.keys(a).length == 2 && a.hasOwnProperty("age") && a.hasOwnProperty("money"))) {
  // yes
}

答案 2 :(得分:1)

使用Object.keys(any_obj).length获取对象中keys的数量。

var x = {name: "something", age: 17, money: 700};
var obj = Object.keys(x);

if((obj.length==2 && x.hasOwnProperty("age") && x.hasOwnProperty("money")) || (obj.length==1 && (x.hasOwnProperty("age") || x.hasOwnProperty("money")))){
    // Condition met.
}

答案 3 :(得分:0)

您的要求不是很清楚,但我认为这是您要做的,或者如果我没有正确理解您的要求,您可能需要稍微更改逻辑。

function customTest(obj, list) {
  var keys = Object.keys(obj);
  var uniq = _.unique(list);
  if (!uniq.length || !keys.length && !uniq.length) {
    return true;
  }
  if (!keys.length || keys.length > uniq.length) {
    return false;
  }
  if (keys.length === uniq.length) {
    return _.isEqual(keys, uniq);
  }
  return uniq.some(function(item) {
    return !_.includes(keys, item);
  });
}

var out = document.getElementById('out');

out.textContent += customTest({}, ['age', 'money']) + '\n'; // false

out.textContent += customTest({
  name: "something",
  age: 17,
  money: 700
}, []) + '\n'; // true

out.textContent += customTest({
  name: "something",
  age: 17,
  money: 700
}, ['age', 'money']) + '\n'; // false

out.textContent += customTest({
  name: "something",
  age: 17,
  money: 700
}, ['age', 'age']) + '\n'; // false

out.textContent += customTest({
  age: 17,
}, ['age', 'age']) + '\n'; // true

out.textContent += customTest({
  name: "something",
  age: 17,
  money: 700
}, ['age', 'oranges']) + '\n'; // false

out.textContent += customTest({
  age: 17,
  money: 700
}, ['age', 'money']) + '\n'; // true

out.textContent += customTest({
  age: 17
}, ['age', 'money']) + '\n'; // true

out.textContent += customTest({
  money: 700
}, ['age', 'money']) + '\n'; // true
<script src="https://rawgit.com/lodash/lodash-compat/3.10.1/lodash.min.js"></script>
<pre id="out"></pre>

如果你更清楚一点,你可能会更加lodash y。当然,您可以使用上面的_.keys_.some代替Object.keysArray.prototype.some