-1

I'm completely new to JQuery and I hope to get some help from SO folks.

This snippet basically generated a random numbers and filled a list along with index values

i.e. [0 10],[1 12],[2 30]... so on

    function getRandomData() {
        if (data.length > 0)
            data = data.slice(1);

        // do a random walk
        while (data.length < totalPoints) {
            var prev = data.length > 0 ? data[data.length - 1] : 50;
            var y = prev + Math.random() * 10 - 5;
            if (y < 0)
                y = 0;
            if (y > 100)
                y = 100;
            data.push(y);
        }

        // zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < data.length; ++i)
            res.push([i, data[i]])
        return res;
    }

Can any one out there help me out with the syntax to retrieve the elements inside data which is in turn present in the res collection.

i.e. i want to know the random number generated each time the function getRandomData
is called

I hope i made it clear Regards

4

3 回答 3

0

类似的东西?

// Save the return value of the function in a variable
var arr = getRandomData();
// Print data to the console
console.log(arr)

// Print the first dataset
console.log(arr[0]) // [0, 29]
// Print only the number of the first set
console.log(arr[0][1])
于 2013-06-19T11:47:45.993 回答
0

你的意思是这样的吗?

var res = [
    [0, 10],
    [1, 12],
    [2, 30]
];
var x = res[0][1]; // returns 10
var y = res[1][1]; // returns 12
var z = res[2][1]; // returns 30

您可以使用以下语法访问任何子数组:

array[first level index][second level index][...nth level index];
于 2013-06-19T11:51:11.753 回答
0

datavar 似乎来自函数范围之外,所以你可以这样做

console.log(data)

虽然我猜你可能会要求@MildlyInteresting 给出的语法?

于 2013-06-19T11:54:03.530 回答