Santosh
Santosh

Reputation: 3807

Check if the property exist in array

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);
}

Upvotes: 0

Views: 77

Answers (4)

Arman Charan
Arman Charan

Reputation: 5797

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)
}

Upvotes: 1

Jayanth
Jayanth

Reputation: 816

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

Upvotes: 0

Zenoo
Zenoo

Reputation: 12880

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);
}

Upvotes: 1

kiranvj
kiranvj

Reputation: 34107

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);
}

Upvotes: 1

Related Questions