如何在Javascript中返回数组中的特定值?

时间:2017-04-09 20:07:44

标签: javascript spotify

我正在尝试从返回大量信息的搜索中返回特定值。引起我问题的原因是:

    results = results.map((res) =>      res[0].tracks.items[10]);

我认为它是res [0]部分,因为错误读取:

jquery.min.js:2 jQuery.Deferred exception: Cannot read property 'tracks' of undefined TypeError: Cannot read property 'tracks' of undefined
at results.map (http://127.0.0.1:60990/script.js:15:59)
at Array.map (native)
at Object.$.when.then.results (http://127.0.0.1:60990/script.js:15:35)
at j (https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:29948)
at k (https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:30262) undefined

我使用了.tracks.items,因为搜索会抛出一个位于轨道内并且有一定数量项目的数组 - 我想返回前10个项目,这就是我在那里放置数字10的原因。< / p>

关于为什么它无法读取资源的任何想法?

更多代码上下文:

const app = {};

app.apiUrl = 'https://api.spotify.com/v1';
//Allow the user to enter names

app.events = function() {
    $("form .submit-btn").click(function (e){
    e.preventDefault();
    let tracks = $('#itrack').val();
    tracks = tracks.split(',');
    let searcht = tracks.map(trackName => app.searchTrack(trackName));

    $.when(...searcht)
        .then((...results)=>{
            results = results.map((res) =>      res[0].tracks.items[1]);
        console.log(results);
    });

0 个答案:

没有答案