如何检查值是否存在或JavaScript对象中是否只存在一个值

时间:2016-11-18 07:02:10

标签: javascript angularjs object

我有一个如下对象:

{"user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [
        {
            "type": "card",
            "accountId": "5299367",
       },
        {
            "type": "Loan",
            "accountId": "5299365", 
        },
        {
            "type": "card",
            "accountId": "8299388", 
        },
     ]}
}

如果用户同时拥有贷款和卡,或只是作为用户产品贷款,我需要知道。

是否有任何内置函数在javascript或angular中找到它。

有人有任何建议如何做到这一点。请帮忙。

5 个答案:

答案 0 :(得分:2)

您可以使用filter数组方法。

var obj = {
    "user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [
        {
            "type": "card",
            "accountId": "5299367",
       },
        {
            "type": "Loan",
            "accountId": "5299365", 
        },
        {
            "type": "card",
            "accountId": "8299388", 
        },
     ]
  }
};
      
var loans = obj.user.products.filter(function(product){
      return product.type === "Loan";
});
      
console.log("Loans: " + loans.length);
      
// supposing that the user has either a Loan or a card. You could
// easily now find out if the user has only loans as below:
      
if(loans.length === obj.user.products.length){
    console.log("The user has only loans");
}else{
    var cards = obj.user.products.length - loans.length;
    console.log("The user has "+loans.length+" Loan(s) and "+ cards+ " Card(s).");
}
      

有关此方法的详情,请查看here

  

我需要知道用户是否同时拥有贷款和信用卡   贷款作为用户产品。

根据上述代码段,使用filter方法并将length的{​​{1}}与loans的{​​{1}}进行比较,您可以回答你的问题。

答案 1 :(得分:0)

您可以循环products并获取唯一类型

var data = {
  "user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [{
      "type": "card",
      "accountId": "5299367",
    }, {
      "type": "Loan",
      "accountId": "5299365",
    }, {
      "type": "card",
      "accountId": "8299388",
    }, ]
  }
}
                 
var result= data.user.products.reduce(function(p,c){
  if(p.indexOf(c.type)<0) p.push(c.type)
  return p;                 
}, [])
    
console.log(result)

答案 2 :(得分:0)

您可以尝试以下

&#13;
&#13;
var obj = {
  "user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [{
      "type": "card",
      "accountId": "5299367",
    }, {
      "type": "Loan",
      "accountId": "5299365",
    }, {
      "type": "card",
      "accountId": "8299388",
    }, ]
  }
};

var results = {};

obj.user.products.reduce(function(oldval, item) {
  oldval[item.type] = true;
  return oldval;
}, results);

console.log(results.card && results.Loan); // paints true
// Additionally, results have all the information about the unique value for type, hence, can be used as per the need
&#13;
&#13;
&#13;

答案 3 :(得分:0)

相同的迭代解决方案,但如果typeLoan不同,会立即为您提供。

&#13;
&#13;
var obj = {"user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [
        {
            "type": "card",
            "accountId": "5299367",
       },
        {
            "type": "Loan",
            "accountId": "5299365", 
        },
        {
            "type": "card",
            "accountId": "8299388", 
        },
     ]}
};
      
function haveDifferentProducts(products){
    var isDiff = false;
    products.forEach(function(product){
       isDiff = product.type != "Loan"; //checks if type not loan
    });
    return isDiff;
}
console.log(haveDifferentProducts(obj.user.products))
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用数组filter()方法。

工作演示:

var jsonObj = {
  "user": {
    "name": "Harry Peter",
    "phoneNumber": "12345",
    "products": [
      {
        "type": "card",
        "accountId": "5299367",
      },
      {
        "type": "Loan",
        "accountId": "5299365", 
      },
      {
        "type": "card",
        "accountId": "8299388", 
      },
    ]   
  }
};

var totalLoan = jsonObj.user.products.filter(function(item){
      return item.type == "Loan";
});


var totalCards = jsonObj.user.products.filter(function(item){
      return item.type == "card";
});

if(totalCards.length < 1 && totalLoan.length < 0) {
  console.log("Only loan");
} else if (totalCards.length > 0 && totalLoan.length < 1) {
  console.log("Only card");
} else {
  console.log("Both card as well as loan");
}