检查属性是否存在于数组中

时间:2018-03-22 08:27:21

标签: javascript

名称// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\": \"New receive address\"}"); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c"; $headers[] = "CB-ACCESS-KEY: <your api key>"; $headers[] = "CB-ACCESS-SIGN: <the user generated message signature>"; $headers[] = "CB-ACCESS-TIMESTAMP: <a timestamp for your request>"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch); 非常罕见,我想检查一下,如果属性存在,我想将其推送到新数组。但是,每次我尝试添加条件时都会给我错误。

attribute_name:"position"循环中的

[0].attribute_name给了我麻烦。 for内可能有也可能没有两个数组。但是如果activity_attributes将它们推送到新数组,我想在第一个数组上进行调用。

attribute_name position

itemloop[i].activity_attributes[0].attribute_name

4 个答案:

答案 0 :(得分:1)

使用时应使用hasOwnProperty方法

if(itemloop[i].activity_attributes[0].hasOwnProperty('attribute_name') && itemloop[i].activity_attributes[0].attribute_name == "position") 

您的代码应该像

if(res.status == "success") {
    var itemloop = res.response.activities;
    var social_post_link = [];
    for(var i=0; i<itemloop.length; i++){
        if(itemloop[i].activity_attributes[0].hasOwnProperty('attribute_name') && itemloop[i].activity_attributes[0].attribute_name == "position") {
            social_post_link.push(itemloop[i].activity_attributes);
        }
    }
    console.log(social_post_link);
}

答案 1 :(得分:1)

您可以使用if('attribute_name' in yourObject)来实现这一目标。

<强>演示。

&#13;
&#13;
var res = {
  status: 'success',
  response: {
    activities : [
      {
        activity_attributes: [
          {
            attribute_name: 'position'
          }
        ]
      },
      {
        activity_attributes: [
          {
            test: 'test'
          }
        ]
      }
    ]
  }
};


if(res.status == "success") {
    var itemloop = res.response.activities;
    var social_post_link = [];
    for(var i=0; i<itemloop.length; i++){
      if( 'attribute_name' in itemloop[i].activity_attributes[0]){ //HERE
        if(itemloop[i].activity_attributes[0].attribute_name == "position") {
            social_post_link.push(itemloop[i].activity_attributes);
        }
      }
        
    }
    console.log(social_post_link);
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

可以将

Array.prototype.filter()Array.prototype.map()结合起来构建基于arrays的新predicated rules,例如attribute_name == 'position'returnvalues }。

请参阅下面的实例。

if (res.status == 'success') {
  const itemloop = res.response.activities
  const social_post_link = itemloop.filter(x => x.attribute_name == 'position').map(x => x.activity_attributes)
  console.log(social_post_link)
}

答案 3 :(得分:0)

而不是activity_attributes[0].attribute_name,请尝试使用activity_attributes[0]['attribute_name'] == 'position'

相关问题