0

The name attribute_name:"position" is very rare and I want to check that if the property exists I want to push it to the new array. However, every time I try to add a condition it gives me errors.

[0].attribute_name inside the for loop is giving me trouble. There may or may not be two arrays inside activity_attributes. But I want to make a call bases on first array, if the itemloop[i].activity_attributes[0].attribute_name push them to new array.

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].attribute_name == "position") {
            social_post_link.push(itemloop[i].activity_attributes);
        }
    }
    console.log(social_post_link);
}
Santosh
  • 3,477
  • 5
  • 37
  • 75
  • why you dont use filter instead and push the required value? – Abslen Char Mar 22 '18 at 08:28
  • can you show me how to do that please? – Santosh Mar 22 '18 at 08:30
  • can you put your array into a code instead of a pictur ? – Abslen Char Mar 22 '18 at 08:30
  • You are directly trying to check using attribute_name that's why giving error in case property not exist. You can check whether property exist or not by referring to this https://stackoverflow.com/questions/11040472/how-to-check-if-object-property-exists-with-a-variable-holding-the-property-name thread. – TechnoCrat Mar 22 '18 at 08:33

4 Answers4

1

Use should use the hasOwnProperty method

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

You code should be like

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);
}
kiranvj
  • 32,342
  • 7
  • 71
  • 76
1

You can use if('attribute_name' in yourObject) to achieve that.

Demo.

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);
}
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • `if( 'attribute_name' in itemloop[i].activity_attributes[0])` this is giving me error because there are some array those doesn't have even `activity_attributes' array – Santosh Mar 22 '18 at 08:39
  • Then also check `if('activity_attribute' in itemloop[i])` – Zenoo Mar 22 '18 at 08:40
  • Thanks your logic worked. I did a little modification `if(itemloop[i].activity_attributes) ` and it worked well. – Santosh Mar 22 '18 at 08:42
1

Array.prototype.filter() and Array.prototype.map() can be combined to construct new arrays based on predicated rules such as attribute_name == 'position' and return child values.

See below for a practical example.

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)
}
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
0

instead of activity_attributes[0].attribute_name ,try using activity_attributes[0]['attribute_name'] == 'position'

Jayanth
  • 746
  • 6
  • 17