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

时间:2022-11-26 12:15:18

I need the value for the key country_code and language_code.

我需要关键country_code和language_code的值。

I have tried

我试过了

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

I get the error:

我收到错误:

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

This is how the code and console logs of the data structure looks like:

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

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 个解决方案

#1


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

#2


0  

try this:

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

#1


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

#2


0  

try this:

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