-2

に私はこのようなオブジェクトがあります:私はfirstIndexの値を取得し、別の配列に入れたい転送値別の配列

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" 
      }, 
     ] 
    } 

]}; 

を。しかし、私はこれを行うには本当に苦労しています。私がしたコードは:

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

animals_array.push(currentClass); 
}; 

私はコードを実行すると、エラーが表示されます:プロパティ 'firstIndex'の未定義を読み取ることができません。ご協力ありがとうございました。

編集:私が実際に使用しているオブジェクトが含まれています。私はいくつかの部分を省略しましたが、基本的に、それは要点です。私が新しい配列に入れたい値は、 "class"プロパティの値です。

+3

リテラルあなたのオブジェクトに問題があります。実際の例を構築してください。 –

+0

現在のコードは、 'Uncaught SyntaxError:Unexpected token 'を生成します。' – Xotic750

+0

'for'文の最後に'; 'を置きます。' i ++'と '{'の間に空白を入れてください。それが現れたら、すぐにループを終了します。 – raina77ow

答えて

0

解決しました。そのミスを指摘してくれてありがとうXotic750。私の質問は次のとおりです。

私が使用した場合:

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

new_array.push(currentIndex); 
}; 

配列が2倍になった(すなわち、それぞれの2を添加しました)。私は

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

に置き換えたときに

しかしそれは正しく動作します。なぜそのような違いがあるのか​​分かりますか?ありがとうございました。

+0

両方のアプローチの結果が異なるはずはありません。しかし、提供されたデータ構造といくつかの(他の)データを処理するコードが一致しないため、何が起こるのか正確には誰にも知られていません。 >> ...これは正しく実行されます。<< - 私は両方のコードスニペットのためにそれを疑います:(a) 'for(var i = 0; i

0

提供されたデータ構造消毒されなければならなかったし、任意のリスト構造 thatsのメンバーはちょうど がmapで最高の処理され、他に1種類/形式から変換する必要があります。

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);

関連する問題