Reputation: 835
I downloaded some JSON which I've converted into an object. I now need to be able to search the second and third level deep and specify which property to search in a variable.
Here is my code attempt.
for (var j = 0; j < currentSnapshot.products.length; j++) {
currentSnapshot.products[j].[firstLayer]; // Is this line kosher?
if (secondLayer) {
for (var i = 0; i < secondLayer.length; i++) {
secondLayer[i]; // Do something.
};
};
};
What is the best way to do this? Thanks for any help.
It looks the the for . . in with a .hasownproperty is what I'm looking for. How do you make this work for the second layer deep -- the properties of the properties?
Upvotes: 0
Views: 34
Reputation: 19759
I'm not sure I understand your requirement, but I'll attempt to answer:
for (var j = 0; j < currentSnapshot.products.length; j++) {
var secondLayer = currentSnapshot.products[j][firstLayer];
if(secondLayer){
for(var i = 0; i < secondLayer.length; i++) {
secondLayer[i]; // Do something.
};
};
};
That is, assuming firstLayer
already refers to a string which is the property you want to access. This is also assuming that currentSnapshot.products
is an array of objects and secondLayer
is also an array.
Also, notice I removed the period in between the square brackets in .products[j].[firstLayer]
, which is a syntax error.
Upvotes: 1