Jayashanka Anushan
Jayashanka Anushan

Reputation: 33

How to Pass an Unknown Number of objects as arguments into JavaScript Function and at last call the function?

I implemented a Javascript function ('setData()') and using another function ('getDataSet(data, id)') it creates the final code. getDataSet(data, id) will be called any number of times and gathering all the data sets, setData() function will be create the chart.

    function getDataSet(data, id){
        //data is a object and name is a element ID
        //do something
    }

    function setData(){
        var chartName;
        var x = new Array;
        for (var i = 0; i < arguments.length; i++) {
            if (i != 0) {
                console.log(arguments[i]);
                x.push(arguments[i]);
            }
            else{
                console.log(arguments[0]);
                chartName = arguments[0];
            }
        }
    }

When i use as follows it works.

setData("viewPoint", getDataSet(x, 'step1'), getDataSet(y, 'step2'), getDataSet(z, 'step3'));

But the number of calls to getDataSet(data, id) function will be change, according to the number of data sets. Number of arguments to setData() function will be change. There for I implemented showCharts() function as follows.

    //Main function
    function showCharts(){ // <- Not working function

        var objects;

        // Run an array to retrieve data and using them need to call getDataSet()
        // finally add their return values as arguments into setData()

        objects += getDataSet(data, id) + ","; // <- create data sets separatly

        setData(objects);   // <- show all data sets in one chart
    }

But still can't figure out how to Pass an Unknown Number of objects as arguments into showCharts() function. Please help me.

Here is the real code. Line #132.

Upvotes: 0

Views: 443

Answers (3)

Jayashanka Anushan
Jayashanka Anushan

Reputation: 33

I modified the code and this way worked.
Used a function as showCharts() to check the code.

            function showCharts(){                
                var arr = new Array;
                var chartLocationID = "chartContainerSales";
                var chartTopic = "Chart Topic";
                var xAxisTitle = "Type code";
                var yAxixTitle = "Qty";

                console.log(xData);
                console.log(yData);
                console.log(zData);

                arr.push(createData(xData, 'MyName'));
                arr.push(createData(yData, 'YourName'));
                arr.push(createData(zData, 'Dog'));
                arr.push(createData(wData, 'New Goat'));

                console.log(arr);
                
                setData(chartTopic, xAxisTitle, yAxixTitle, chartLocationID, arr);
            }

Upvotes: 0

Andrey Smolko
Andrey Smolko

Reputation: 1154

I think you may significantly simplify if make a parameter which accepts an array for setData function:

    function showCharts(){
        let arr;
        arr.push(getDataSet(data, id))
        setData(arr);
    }

    function setData(array){
        var chartName;
        var x = new Array;
        for (var i = 0; i < array.length; i++) {
            if (i != 0) {
                console.log(array[i]);
                x.push(array[i]);
            }
            else{
                console.log(array[0]);
                chartName = array[0];
            }
        }
    }

Upvotes: 3

Neelkant Jain
Neelkant Jain

Reputation: 11

you can use

showCharts(...values)
{
    //values will be an array of the variables you passed that you can use
}

Upvotes: 1

Related Questions