简体   繁体   English

如何访问数组中的所有对象

[英]How to access all objects inside array

I am working on a project where I pull data from an API and input it into the DOM.我正在做一个项目,我从 API 中提取数据并将其输入到 DOM 中。 One of the API responses returns an array of objects and I need to get the 'name' value from each of the objects and input it into the DOM here is a response example: API 响应之一返回一个对象数组,我需要从每个对象中获取“名称”值并将其输入到 DOM 中,这是一个响应示例:

"languages": [{
    "iso639_1": "es",
    "iso639_2": "spa",
    "name": "Spanish",
    "nativeName": "Español"
}]

Here is my function这是我的 function

container.addEventListener('click', e => {
    searchFilter.innerHTML = '';
    
    const clicked = e.target.getAttribute('data-name');

    const getCountry = fetch(`https://restcountries.eu/rest/v2/name/${clicked}`)
        .then(response => response.json())
        .then((clickedCountry) => {
            clickedCountry.forEach(i => {
                container.innerHTML = '';
                detail.innerHTML += `
                    <button id="btn">Back</button>

                    <h1>${i.name}</h1>
                    <div class="detail-container">
                        <div class="dets">
                            <img src="${i.flag}">
                        </div>
                        <div class="dets">
                            <div>
                                <h3>Native Name: ${i.nativeName}</h3>
                                <h3>Population: ${i.population}</h3>
                                <h3>Region: ${i.region}</h3>
                                <h3>Sub Region: ${i.subregion}</h3>
                                <h3>Capital: ${i.capital}</h3>
                            </div>
                            <div>
                                <h3>Top Level Domain: ${i.topLevelDomain}</h3>
                                <h3>Currencies: ${i.currencies[0].name}</h3>
                                <h3>Languages: ${i.languages}</h3>
                            </div>
                        </div>
                    </div>`;
                search.innerHTML = '';
            })
        });

})

If I run this code it will return the array, if I run i.languages.name I get undefined as I first need to access the array.如果我运行此代码,它将返回数组,如果我运行 i.languages.name 我会得到未定义,因为我首先需要访问该数组。 Is there anyway to simply return all of the object name values?无论如何都可以简单地返回所有 object 名称值? I have tried to find a way to use i.languages[].name but this only returns one object and I also tried another forEach inside the function but couldn't get it to work.我试图找到一种使用 i.languages[].name 的方法,但这只返回一个 object 并且我还在 function 中尝试了另一个 forEach 但无法让它工作。

If you want to access all the names in the languages array of objects, you need to use Array.prototype.map :如果要访问languages对象数组中的所有名称,则需要使用Array.prototype.map

i.languages.map(language => language.name);

That will return an array of language names extracted from the array of objects.这将返回从对象数组中提取的语言名称数组。 However, if you want to print it out in a human readable format, perhaps you can also do:但是,如果您想以人类可读的格式打印出来,也许您也可以这样做:

<h3>Languages: ${i.languages.map(language => language.name).join(', ')}</h3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM