0

I am trying to return all it (string) from within things (1-10 objects) and stuff (1-10 objects). I have noticed that I am unable to do it with wildcard but am wondering if there's another way for me to do it?

I would like to return an array with all of the it.

response.things[0].stuff[0].it will return string from my first object is each of things and stuff, but

$.get("https://page.json", function(response) {
  var substr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  var arrayIts = [];
  $.each(substr , function(index, val) { 
    var arrayIts = response[index];
    console.log(arrayIts.things[index].stuff[val].it);
  });
});

Above will return all it until it returns undefined and then it stops.

How could I return all it and possibly ignore/skip undefined?

My solution so far would look something like below.

$.getJSON('https://jsonplaceholder.typicode.com/posts', function(data) {
   var arrayData = [];
  try {arrayData.push("<li>"+data[0].id+"</li>");}catch(e){}
  try {arrayData.push("<li>"+data[1].id+"</li>");}catch(e){}
    try {arrayData.push("<li>"+data[2].id+"</li>");}catch(e){}
   try {arrayData.push("<li>"+data[100].id+"</li>");}catch(e){arrayData.push("<li>skipped</li>")}
    try {arrayData.push("<li>"+data[3].id+"</li>");}catch(e){}
    try {arrayData.push("<li>"+data[99].id+"</li>");}catch(e){}
  document.getElementById('listId').innerHTML = arrayData.join("");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="listId">empty</ul>
Joe Berg
  • 381
  • 3
  • 16
  • what is `response` ? – TKoL Jun 13 '19 at 13:16
  • Hello Joe, maybe this will help: https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript/24190282 Why don't you use just a reduce, check/descart undefined and accumulate all the "it" in an Array. – gengns Jun 13 '19 at 18:05

1 Answers1

0

Just use an if .

If (response[index]) arrayIts.push(response[index]);

If response[index]) is defined then it's true. Else it's false.

Feronimus
  • 123
  • 10
  • (sorry for bad formatting in comment below) Hi Fero, Thanks for your answer. Below is how I tried it out, but for me it still stops when the path return undefined. for(var i=0; i < arrayIts.length; i++) { if (arrayIts.things[index].stuff[index].it) console.log(arrayIts.things[index].stuff[index].it); } As soon as **it** is undefined/not exist, it will stop. So it will only return the strings before the undefined occurs. TypeError: undefined is not an object (evaluating 'arrayIts.things[index].stuff[index].it') – Joe Berg Jun 16 '19 at 13:20
  • Oh, now i see it. Your problem is that you are not just trying to get a value from a single object, but from a nested one. In that case i would go and check this out. [Test for existence of nested JavaScript object key](https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key) – Feronimus Jun 17 '19 at 08:59
  • Thanks Fero, that page helped me understand my issue a bit more, but the only solution that I am able to think of is to wrap inside a try and catch the error(not display it) and repeat for ever single value. My code will end up being thousands of line of code, but in the end will give me the result I want. But could you have a look at this example I created in jsfiddle, to see if there's another solution for it? https://jsfiddle.net/3pegrzkc/ – Joe Berg Jun 17 '19 at 22:33
  • 1
    `for (i = 0; i < data.length; i++) { try { arrayData.push("
  • "+data[ i].id+"
  • "); } catch(e){ arrayData.push("
  • skipped
  • ") } }` – Feronimus Jun 18 '19 at 09:59
  • Hi Feronimus, thanks for the solution. It seems to work well on jsfiddle with the example json url, but when I try it with my actual url it returns nothing (no error as well). This only happens with the loop, or else my json url works well. E.g. **data[0].id** would return **1** without the loop but **data[i].id** returns nothing when I use the loop. Do you have any idea why this might be? I wish I could share the actual url. – Joe Berg Jun 18 '19 at 17:10
  • Share with me the code for the loop. Either your doing 0 iterations( data.length ) or your not pushing anything. Try to add some console.log(); to see what the variables are at each state and walk your problem through – Feronimus Jun 18 '19 at 17:13
  • 1
    My point is that if data[i].id exists, then it should push something. If it does not exist, then it should push "
  • skipped
  • " .. Since , as you say, the arrayData is empty after the iteration, then no loop happened. – Feronimus Jun 18 '19 at 17:23
  • I noticed a difference in console that my url returns **responseJSON: Object** but the one in my jsfiddle returns **responseJSON: Array** for **data**. So to solve it, I think I just need to use **data.second.length** in the loop as **second** is an array and not an object. So now it returns the first string from the loop when I do like this **data.second[i].third[i].string** so it returns same as when I do **data.second[0].third[0].string** without the loop. – Joe Berg Jun 18 '19 at 18:00
  • I dont get where the problem is! Keep working on it. and if you want different iterations like **data.second[0].third[2].string** you can always call may For one inside the other – Feronimus Jun 18 '19 at 18:31
  • Thanks a lot for all the help! I will keep on trying to figure out where the issue might be at. I know at least part of the issue now thanks to you. :) – Joe Berg Jun 18 '19 at 18:54