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>