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);
}
4
  • why you dont use filter instead and push the required value? Commented Mar 22, 2018 at 8:28
  • can you show me how to do that please? Commented Mar 22, 2018 at 8:30
  • can you put your array into a code instead of a pictur ? Commented Mar 22, 2018 at 8: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 stackoverflow.com/questions/11040472/… thread. Commented Mar 22, 2018 at 8:33

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
Then also check if('activity_attribute' in itemloop[i])
Thanks your logic worked. I did a little modification if(itemloop[i].activity_attributes) and it worked well.
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);
}

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.