0

I'm trying to return the results from each list, using an array of variables containing the names of the lists.

var filters = ["colour", "size", "len", "s_length", "occasion"];
var colour = ["black", "white", "blue", "brown", "gold",
 "green", "grey", "multi", "nude", "orange", "pink",
 "purple", "red", "silver", "yellow"];
var size = ["XS", "S", "M", "L", "UK 6", "UK 8",
 "UK 10", "UK 12", "UK 14", "UK 16", "UK 18+"];
var len = ["maxi", "midi", "mini"];
var s_length = ["sleeveless", "short", "3/4", "long"];
var occasion = ["casual", "party/evening", "work"];
for (var h = filters.length; h--;) {
 console.log(filters[h])
 for (var i = filters[h].length; i--;) {
 console.log(filters[h[i]])
 }
}

I'm used to python, and this seems like it would work as I want, but instead of returning the array items I get an undefined result for filters[h[i]]

What am I doing wrong here? TIA

asked Feb 3, 2016 at 1:56
1
  • That is because h is an integer and doing h[i] will return undefined. Commented Feb 3, 2016 at 2:14

4 Answers 4

1

You cannot get a reference to a local variable from a string in JavaScript. There is no way to get to the array in colour if all you have is the string "colour".

What you can do is have your filters all in an object ("dictionary" for Python people):

var filters = {
 colour: [
 "black", "white", "blue", "brown", "gold",
 "green", "grey", "multi", "nude", "orange", "pink",
 "purple", "red", "silver", "yellow"
 ],
 size: [
 "XS", "S", "M", "L", "UK 6", "UK 8",
 "UK 10", "UK 12", "UK 14", "UK 16", "UK 18+"
 ],
 len: [
 "maxi", "midi", "mini"
 ],
 s_length: [
 "sleeveless", "short", "3/4", "long"
 ],
 occasion: [
 "casual", "party/evening", "work"
 ]
};

then iterate on that to pick up the various values.

Object.keys(filters).forEach(function(filterName) {
 filters[filterName].forEach(function(option) {
 console.log(option);
 });
});
answered Feb 3, 2016 at 2:01
Sign up to request clarification or add additional context in comments.

1 Comment

@NeilVillareal: I think you replied to a wrong person...? I never mentioned h[i]...
1

filters is an array of strings. When you are doing:

 filters[h[i]]

You are trying to access the property i of the element h of the array filters. As filters is an array of strings, h will point to a string, and i will try to dig into that string. And it will fail to do so (won't find a thing).

Probably you should declare all the other arrays:

 var colour = ...
 var size = ...
 var len = ...

And last do this:

var filters = [colour, size, len, s_length, occasion];

So now filters is an array of arrays, and you can dig into a element of each of those arrays

answered Feb 3, 2016 at 2:00

Comments

0

To achieve the same logic correctly in JavaScript you can do it like this

var filters = ["colour", "size", "len", "s_length", "occasion"];
var variables = {
 colour : ["black", "white", "blue", "brown", "gold",
 "green", "grey", "multi", "nude", "orange", "pink",
 "purple", "red", "silver", "yellow"],
 size : ["XS", "S", "M", "L", "UK 6", "UK 8",
 "UK 10", "UK 12", "UK 14", "UK 16", "UK 18+"],
 len : ["maxi", "midi", "mini"],
 s_length : ["sleeveless", "short", "3/4", "long"],
 occasion : ["casual", "party/evening", "work"],
};
for (var h = filters.length; h--;) {
 console.log(filters[h])
 for (var i = variables[filters[h]].length; i--;) {
 console.log(variables[filters[h]][i])
 }
}
answered Feb 3, 2016 at 2:06

Comments

0

You might want to try this. You can test it here https://jsfiddle.net/Lk9xcex2/.

var filters = ["colour", "size", "len", "s_length", "occasion"];
var colour = ["black", "white", "blue", "brown", "gold",
 "green", "grey", "multi", "nude", "orange", "pink",
 "purple", "red", "silver", "yellow"];
var size = ["XS", "S", "M", "L", "UK 6", "UK 8",
 "UK 10", "UK 12", "UK 14", "UK 16", "UK 18+"];
var len = ["maxi", "midi", "mini"];
var s_length = ["sleeveless", "short", "3/4", "long"];
var occasion = ["casual", "party/evening", "work"];
for (var h = filters.length; h--;) {
 console.log(eval(filters[h]));
}
answered Feb 3, 2016 at 2:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.