如何在jquery中覆盖对象的属性(这是一个数组)

时间:2015-05-19 12:40:04

标签: javascript jquery json

我有一个object-literal,options,它有一个名为datapoints的属性,它是一个数组。我正在尝试将这个数组替换为我从JSON文件中获取的数组(粘贴在下面)。

tempactivation函数中,我有这段代码options.data[0].dataPoints = items;

我的意思是

dataPoints: [
    { x: 5, y: 10 },
    { x: 10, y: 12 },
    { x: 15, y: 8 },
    { x: 20, y: 14 }
]

必须在option对象本身上更改为:

dataPoints: [
    {x: 144949, y : 56},
    {x: 144950, y : 12},
    {x: 144951, y : 81},
    {x: 144952, y : 14}
]

但是,该属性未按预期更改值。

这是我尝试的代码。

$(function () {
    function waetherName(){
        var thisOut = 'Mon- '+'13/05/2014';
        return thisOut;
    }

    function tempActivation(){
        var items = [];
        $.getJSON( "JSON/Temperature.json", function( data ) {
            $.each(data.Temperature, function( index, value ) {
                items.push(value);
                console.log(value);
                console.log(items[index] + ' Ar');
            });

            options.data[0].dataPoints = items;

            console.log(options.data[0].dataPoints);
        }); 
    }


    tempActivation();


    var options = {
        title: {
            text: "Weather Chart for Bangalore"
        },
        animationEnabled: true,
        data: [
            {
                type: "spline",
                dataPoints: [
                    { x: 5, y: 10 },
                    { x: 10, y: 12 },
                    { x: 15, y: 8 },
                    { x: 20, y: 14 },
                    { x: 25, y: 6 },
                    { x: 30, y: 24 },
                    { x: 35, y: -4 },
                    { x: 40, y: 10 },
                    { x: 45, y: 10 },
                    { x: 50, y: 10 },
                    { x: 55, y: 10 },
                    { x: 60, y: 10 },
                    { x: 65, y: 10 }
                ]
            }
        ]
    };

    options.title.text = waetherName();
});

JSON文件

{    
    "_id": {
        "$oid": "555468b14a86e4cc2fdd62aa"
    },
    "Date": "20150514",
    "Temperature": [    
        {"x": "144949", "y" : "56"},
        {"x": "144950", "y" : "12"},
        {"x": "144951", "y" : "81"},
        {"x": "144952", "y" : "14"},
        {"x": "144953", "y" : "30"},
        {"x": "144954", "y" : "31"},
        {"x": "144955", "y" : "40"},
        {"x": "144956", "y" : "30"},
        {"x": "144957", "y" : "54"},
        {"x": "144958", "y" : "45"},
        {"x": "144959", "y" : "1"},
        {"x": "145000", "y" : "50"},
        {"x": "145001", "y" : "21"},
        {"x": "145002", "y" : "18"},
        {"x": "145003", "y" : "43"},
        {"x": "145004", "y" : "54"}
    ]
}

0 个答案:

没有答案