简体   繁体   中英

reading array key values with a keyname

Just an easy question, I have this code:

$("#renderBtn").click(
    function () {
        var data =  {"first":[20000, 14000, 12000, 15000, 18000, 19000, 22000], "second":[12000, 11000, 18000, 12000, 19000, 14000, 26000]}
        labels =  ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
            for ( x in data) {
                console.log(x);
                console.log(data.x);
                console.log(data.first);
            }
        }
);

as a result x is array key names which are "first" and "second", I can get values by calling data.first but the return out of data.x is undefined , how I can get values by calling data from x???

You have to use two for loops one for data.first and another data.second beacuse your data is an object. for loop is used to loop through arrays. for ( const x in data.first){console.log(data.first[x]);} You shoud get the values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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