1

I am new to JavaScript, tried below code and not able to get it. I have gone through many other post but didn't work.

Output: Check if AUTO damage is present. If Yes, get count and display First & Last for the same.

var myObj, i, x = "";
myObj = {
  "Initial": {
    "claim": [{
      "first": "abc",
      "last": "xyz",
      "damage": {
        "auto": true,
        "manual": true
      }
    }, {
      "first": "mne",
      "last": "odc",
      "damage": {
        "manual": true
      }
    }, {
      "first": "svc",
      "last": "tre",
      "damage": {
        "auto": true
      }
    }, {
      "first": "tre",
      "last": "hdf",
      "damage": {}
    }]
  }
}

var myObj = myObj.Initial.claim;

console.log(myObj);

for (i = 0; i < myObj.length; i++) {
  x += myObj.first[i] + "<br>";
  x += myObj.damage.auto[i] + "<br>";
}

document.getElementById("demo").innerHTML = x;
<p id="demo"></p>

1
  • what do you mean by "get count" ? Commented Feb 12, 2017 at 7:12

3 Answers 3

2

First myObj.Initial.claim is an array not an object, it's better to name it myArr instead of myObj.

Before accessing an object property which can be present or not, you have to check for it before doing that using myObj.hasOwnProperty method.

var myObj, i, x = "";
myObj = {
  "Initial": {
    "claim": [
      {
        "first": "abc",
        "last": "xyz",
        "damage": {
          "auto": true,
          "manual": true
        }
      }, 

      {
        "first": "mne",
        "last": "odc",
        "damage": {
          "manual": true
        }
      },

      {
        "first": "svc",
        "last": "tre",
        "damage": {
          "auto": true
        }
      }, 

      {
        "first": "tre",
        "last": "hdf",
        "damage": {}
      }
    ]
  }
}

var myArr = myObj.Initial.claim;

console.log(myArr);

for (i = 0; i < myArr.length; i++) {
  x += myArr[i].first + "<br>";

  if(myArr[i].damage.hasOwnProperty("auto")) {
    x += myArr[i].damage.auto + "<br>";
  }
}


document.getElementById("demo").innerHTML = x;
<p id="demo"></p>

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

1 Comment

Thanks, Why is it giving as undefined in second line console.
0

You can use the hasOwnProperty function to check whether the damage is auto or not, check the below code snippet.

'use strict';

var claims = {
  "Initial": {
    "claim": [{
      "first": "abc",
      "last": "xyz",
      "damage": {
        "auto": true,
        "manual": true
      }
    }, {
      "first": "mne",
      "last": "odc",
      "damage": {
        "manual": true
      }
    }, {
      "first": "svc",
      "last": "tre",
      "damage": {
        "auto": true
      }
    }, {
      "first": "tre",
      "last": "hdf",
      "damage": {}
    }]
  }
};

var result = [];
for (var i = 0; i < claims.Initial.claim.length; i++) {
  if (claims.Initial.claim[i].damage.hasOwnProperty('auto')) {
    result.push(claims.Initial.claim[i]);
  }
}

console.log(result);

var x = '';
for (var i = 0; i < result.length; i++) {
  x += result[i].first + "<br>";
  x += result[i].damage.auto + "<br>";
}

document.getElementById("demo").innerHTML = x;
<p id="demo"></p>

Comments

0

You could use hasOwnProperty to check if auto exists.

for example:

myObj.forEach(function(val) {
    if (val.damage.hasOwnProperty('auto') && val.damage.auto) {
        console.log(val.first, val.last);
    }
});

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.