-2

I have an object like this:

var animals_data = {
category : [
    {
        class : "Reptiles",
        animals : [
            {
                image1 : "some image",
                image2 : "some image 2",
                name : "Snake",
                description : "some description"
            },
            {
                image1 : "another image",
                image2 : "another image 2",
                name : "Crocodilia",
                description : "another description"
            },
        ]
    },
    {
        class : "Mammals",
        animals : [
            {
                image1 : "more image",
                image2 : "more image 2",
                name : "Cat",
                description : "more description"
            },
            {
                image1 : "image",
                image2 : "image 2",
                name : "Dog",
                description : "long description"
            }
        ]
    },
    {
        class : "Birds",
        animals : [
            {
                image1 : "bird image",
                image2 : "bird image 2",
                name : "American flamingo",
                description : "bird description"
            },
            {
                image1 : "another bird image",
                image2 : "another bird image 2",
                name : "Atlantic puffin",
                description : "another bird description"
            },
        ]
    }

]};

I want to get the values of the firstIndex and put them in a different array. However, I am having a really hard time doing this. The code I did is:

for (var i = 0; i < animals_data.category.length; i++) {
var currentIteration = animals_data.category[i];    
var currentClass = currentIteration.class;

animals_array.push(currentClass);
};

When I run the code the error says: Cannot read property 'firstIndex' of undefined. Thank you for your help.

Edit: included the actual object I am working with. I have omitted some parts but basically, that's the gist. The values I want to put in a new array are the ones in the "class" property.

5
  • 3
    There's a problem with your object literal. Please build a working example. Commented Sep 7, 2016 at 16:10
  • Current code produces Uncaught SyntaxError: Unexpected token ] Commented Sep 7, 2016 at 16:15
  • Drop ; at the end of for statement: there should be nothing but whitespace between i++) and {. As it stands, you immediately finish your loop. Commented Sep 7, 2016 at 16:17
  • Edited it to include the object I am working with. Commented Sep 7, 2016 at 16:25
  • Still not reproducible with your example. Uncaught ReferenceError: obj is not defined Commented Sep 7, 2016 at 16:27

2 Answers 2

0

Solved it. Thank you Xotic750 for pointing out that mistake. My question is:

when I used:

for (var i = 0; i < obj.firstKey.length; i++); {
var currentIteration = obj.firstKey[i];
var currentIndex = currentIteration.firstIndex;

new_array.push(currentIndex);
};

The array was doubled (i.e two of each was added).

However when I replaced it with

new_array.push(obj.firstKey[i].firstIndex;

It runs correctly. May I know why there is such a difference? Thank you.

Sign up to request clarification or add additional context in comments.

1 Comment

The results of both approaches should not differ. But nobody knows exactly what does happen since your provided data structure and the code that does process some (other) data do not match. >>... It runs correctly ...<< - I doubt it for both code snippets: (a) for (var i = 0; i < obj.firstKey.length; i++); { ... - last semicolon will prevent running the loop as intended; (b) new_array.push(obj.firstKey[i].firstIndex; - closing parenthesis is missing.
0

The provided data structure had to be sanitized and any list structure thats members just need to be transformed from one type/form into another, is processed best by map.

var
    animalData = {
        categories: [{

            class   : "Reptiles",
            animals : [{

                image1 : "some image",
                image2 : "some image 2",
                name : "Snake",
                description : "some description"
            }, {
                image1 : "another image",
                image2 : "another image 2",
                name : "Crocodilia",
                description : "another description"
            }]

        }, {
            class   : "Mammals",
            animals : [{

                image1 : "more image",
                image2 : "more image 2",
                name : "Cat",
                description : "more description"
            }, {
                image1 : "image",
                image2 : "image 2",
                name : "Dog",
                description : "long description"
            }]

        }, {
            class   : "Birds",
            animals : [{

                image1 : "bird image",
                image2 : "bird image 2",
                name : "American flamingo",
                description : "bird description"
            }, {
                image1 : "another bird image",
                image2 : "another bird image 2",
                name : "Atlantic puffin",
                description : "another bird description"
            }]
        }]
    },

    animalClassNameList = animalData.categories.map(function (category) {
        return category.class;
    });


console.log("animalData : ", animalData);
console.log("animalClassNameList : ", animalClassNameList);

Comments

Your Answer

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.