0

This is the JSON I get

entityData = [
   {
      "is_place":false,
      "type":[
         "Person"
      ],
      "is_organization":false,
      "name":[
         "Kevin Presto"
      ],
      "occurrences":38,
      "is_person":true,
      "jobTitle":[
         "Vice President"
      ],
      "label":"Presto"
   },
   {
      "is_place":false,
      "label":[
         "Paris salon",
         "Paris Salonu",
         "Salon (mostra)"
      ],
      "occurrences":1,
      "is_person":false
   },
   {
      "is_place":false,
      "label":"IEP Paris",
      "is_organization":true,
      "occurrences":1,
      "is_person":false
   }
]

But it comes as a text format,

   if (entityData === Array) {
      console.log('entityData is Array!');
   } else {
      console.log('Not an array');
   }

Then I loop through to find if is_place is true

for (_i = 0, _len = entityData.length; _i < _len; _i++) {
  entity = entityData[_i];
  console.log('for loop going '+entity)
  if (entity.is_place === true) {
    console.log('place found found')
  }
}

But above code log each and every character of the entityData Where I'm wrong.

UPDATE: I was following @shreedhar answer and got following error, any idea where I'm doing wrong.

TypeError: entityData.forEach is not a function
entityData.forEach(function(entity,i){
zerkms
  • 249,484
  • 69
  • 436
  • 539
samuk
  • 157
  • 1
  • 3
  • 19
  • 6
    Use JSON.parse(str) to convert the string data in the variable str to an object. – user2182349 Sep 10 '15 at 11:43
  • entity in this line `console.log('for loop going '+entity)` is an object appending to string gived [Object object] instead do this `console.log('for loop going ',entity)` and JSON you pasted is valid JSON, it is not a string , but you have string in your real code. so use `JSON.parse(entityData)` as @user2182349 said. – J Santosh Sep 10 '15 at 11:45
  • 4
    Also `entityData === Array` will never be true, see: [check-if-object-is-array](http://stackoverflow.com/questions/4775722/check-if-object-is-array) – Hacketo Sep 10 '15 at 11:45

2 Answers2

0

Since is_place property is false in all objects of array, code within if block within for loop will not be executed.

for (var _i = 0, _len = entityData.length; _i < _len; _i++) { var entity = entityData[_i]; console.log('for loop going '+ entity) if (entity.is_place === true) { console.log('place found') } else { console.log('place not found'); } }

always prefer array.forEach to iterate an array.

entityData.forEach(function(entity){ console.log('entity is', entity); if(entity[i].is_place){ console.log('Place found', entity[i]); } else { console.log('Place not found'); } });

Shreedhar
  • 5,502
  • 3
  • 22
  • 27
  • @sheedhar Im getting following error when I follow you, 'TypeError: entityData.forEach is not a function entityData.forEach(function(entity,i){' – samuk Sep 11 '15 at 07:29
  • @shreedhar i think you missed this point in OP `But above code log each and every character of the entityData Where I'm wrong.` – J Santosh Sep 11 '15 at 18:23
0

try this

entityData = '[{"is_place":false,"type":["Person"],"is_organization":false,"name":["Kevin Presto"],"occurrences":38,"is_person":true,"jobTitle":["Vice President"      ],"label":"Presto"},{"is_place":false,"label":["Paris salon","Paris Salonu","Salon (mostra)"],"occurrences":1,"is_person":false},{"is_place":false,"label":"IEP Paris","is_organization":true,"occurrences":1,"is_person":false}]';
entityData = JSON.parse(entityData);
for (_i = 0, _len = entityData.length; _i < _len; _i++) {
  entity = entityData[_i];
  console.log('for loop going ', entity)
  if (entity.is_place === true) {
    console.log('place found found')
  }
}
J Santosh
  • 3,808
  • 2
  • 22
  • 43