0

My loop function is as below

 var resources= jsonObj.entry;
 var resourceType;
 var i;
 for (i = 0; i < resources.length; i++) {
  resourceType += resources[i];
  }

  console.log(resourceType)

if i do jsonObj.entry[0] i get the first entry so i implemented for-loop to get all the entries but console.log on resourceType prints the following

Console log result

jsonObj.entry

Sam
  • 387
  • 4
  • 17
  • 1
    Check out [What is the difference between ( for… in ) and ( for… of ) statements in JavaScript?](https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-statements-in-jav) and.. aren't you forgetting to use the index on your code snippet there? – rmjoia Feb 18 '20 at 10:31
  • 1
    along with missing index like `resorces[i]` you should initialize `resourceType` with an empty string, like `resourceType = ""`. – Gulam Hussain Feb 18 '20 at 10:33
  • Are you wanting to change `[object Object]` or get rid of the `undefined` at the start? – Jacob Feb 18 '20 at 10:34
  • @sam is this what you're trying to achive? [example](https://stackblitz.com/edit/typescript-epz9jd) – rmjoia Feb 18 '20 at 10:36
  • You seem to be concatenating objects. (Doing something like `{}.toString() + {}.toString()`) Is this what you want? – evolutionxbox Feb 18 '20 at 10:36
  • `resources` is an array of objects. If you want to concatenate specific properties of the object, try `resourceType += resources[i].propertName`. It's unclear what you're trying to concatenate – adiga Feb 18 '20 at 10:41
  • @rmjoia your example code prints out number of entries i have attached another image my data is further nested in entries. – Sam Feb 18 '20 at 10:44
  • @sam I changed it a bit, I'm not getting what you want to do. your question says what you get, doesn't say what you would like to get... – rmjoia Feb 18 '20 at 10:45
  • @rmjoia the problem is that the resource i am trying to get is highly nested. it sits in jsonObj.entry[0].resource all i need is a for loop so i don't have to do jsonObj.entry[0].resource jsonObj.entry[1].resource jsonObj.entry[2].resource your code is still printing [object, object] for me. – Sam Feb 18 '20 at 11:05
  • @sam that's what evolutionxbox is saying, it's printing that, because you're concatenating a string, if you want to create a new object, or push the object to an array, you shouldn't concatenate a string, I updated the example using an array, check it out. If you want to concatenate to a string you can call the .toString() as evoutionbox is saying.. what is the expected outcome? what would you like to see? can you provide an example? – rmjoia Feb 18 '20 at 11:12
  • @sam I updated my example again, with string and array... I'm sorry but I don't know yet the expected outcome... – rmjoia Feb 18 '20 at 11:20
  • @rmjoia if you see the attach image after fullUrl: there is resource which i need to loop to get all resources – Sam Feb 18 '20 at 11:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208040/discussion-between-rmjoia-and-sam). – rmjoia Feb 18 '20 at 11:30

1 Answers1

1

So, one way to do it is

var resources = jsonObj.entry;
var resourceTypeArray = [];
var resourceType = "";

for (let item = 0; item <= resources.length; item++) {
  // This will remove all cases where the item doesn't exist and/or resource doesn't exist
  if(resources[item] && resources[item].resource){
      resourceTypeArray.push(resources[item].resource);  
      resourceType += JSON.stringify(resources[item].resource);
  }
}

// printing out to the console the array with resources
console.info(resourceTypeArray);

// printing out to the console the string with the concatenation of the resources
console.info(resourceType);

I also created a StackBlitz with the working solution and your json file kindly provided.

hope it helps.

rmjoia
  • 962
  • 12
  • 21