如何在数组中的这个javascript对象中获取键的值?

时间:2017-02-15 16:26:24

标签: javascript arrays object key

我需要键country_code和language_code的值。

我试过了

sourceLanguageArray[0].$.country_code;
sourceLanguageArray[0].$.language_code;

我收到错误:

console.log(sourceLanguageArray[0].$.country_code);
            ^
ReferenceError: sourceLanguageArray is not defined

这就是数据结构的代码和控制台日志的样子:

source_language (includes country_code and language_code)
const sourceLanguageArray = _.map(ontomlClass, 'source_language');

console.log(sourceLanguageArray);
// => [ [ { '$': [Object] } ], [ { '$': [Object] } ],[ { '$': [Object] } ] ]
// => [ { '$': [Object] } ]

console.log(sourceLanguageArray[0]);
// => [ { '$': { country_code: 'US', language_code: 'en' } } ]

2 个答案:

答案 0 :(得分:1)

var sourceLanguageArray = [[ { '$': { country_code: 'US', language_code: 'en' } } ]];

console.log("sourceLanguageArray[0]:", sourceLanguageArray[0]);

console.log("country code:", sourceLanguageArray[0][0].$.country_code);
console.log("language code:", sourceLanguageArray[0][0].$.language_code);

答案 1 :(得分:0)

试试这个:

sourceLanguageArray.forEach(function(item){
    console.log(item[0]['$'][country_code]);
    console.log(item[0]['$'][language_code]);
})