javascript - 如何将数据推送到 JavaScript 对象文字

标签 javascript angularjs javascript-objects

我想将数据推送到 JavaScript 对象文本中。但是,无论如何也找不到。我正在尝试以下代码。

var wd = { item: [] }
wd.item = {
    country: weatherData.query.results.weather.rss.channel.location.country,
    state: weatherData.query.results.weather.rss.channel.location.city,
    displayState: weatherData.query.results.weather.rss.channel.location.city + ', ' + weatherData.query.results.weather.rss.channel.location.country,
    data: [
    {
        date: weatherData.query.results.weather.rss.channel.item.forecast[0].date,
        day: weatherData.query.results.weather.rss.channel.item.forecast[0].day,
        high: weatherData.query.results.weather.rss.channel.item.forecast[0].high,
        low: weatherData.query.results.weather.rss.channel.item.forecast[0].low,
        text: weatherData.query.results.weather.rss.channel.item.forecast[0].text
    }
    ]
};

我尝试使用push()函数,但出现错误。

添加一项后,结果如下。

 Object {item: Object}item: Objectcountry: "India"data: Array[1]0: Object$$hashKey: "object:254"date: "4 Jan 2015"day: "Sun"high: "84"low: "66"text: "Partly Cloudy"__proto__: Objectlength: 1__proto__: Array[0]displayState: "Bangalore, India"state: "Bangalore"__proto__: Object__proto__: Object

但是,此后我无法添加新数据。有人可以帮忙吗?

以下代码现在可以运行。

var wd = { item: [] };
var
                location = weatherData.query.results.weather.rss.channel.location,
                forecast = weatherData.query.results.weather.rss.channel.item.forecast[0];

            wd.item.push({
                country: location.country,
                state: location.city,
                displayState: location.city + ', ' + location.country,
                data: {
                    date: forecast.date,
                    day: forecast.day,
                    high: forecast.high,
                    low: forecast.low,
                    text: forecast.text
                }
            });
            console.log(wd);

感谢大家的帮助。

最佳答案

第二行代码用普通对象覆盖 wd.item 处的数组。我假设您打算将对象添加到数组中。

wd.item.push({
  // your big object
});
<小时/>

正如 @MightyPork 所说,如果您使用变量来保存目标嵌套对象,您的代码将更具可读性。

var wd = { item: [] };
var loc = weatherData.query.results.weather.rss.channel.location;
var forecast = weatherData.query.results.weather.rss.channel.item.forecast;

wd.item.push({
    country: loc.country,
    state: loc.city,
    displayState: loc.city + ', ' + loc.country,
    data: [
        {
            date: forecast[0].date,
            day:  forecast[0].day,
            high:  forecast[0].high,
            low:  forecast[0].low,
            text:  forecast[0].text
        }
    ]
});

关于javascript - 如何将数据推送到 JavaScript 对象文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769113/

相关文章:

asp.net-mvc-4 - 带有外部数据和 TypeScript : compile error "Cannot set property ' gridDim' of undefined"的 Ng-grid

javascript - 将模型传递给自定义 Angular Directive(指令)进行验证

javascript - Websocket 握手失败 404(golang 服务器)

javascript - 如何使用内存流在上传之前预览图像

javascript - phonegap - 菜单按钮不会触发任何东西

javascript - 如果条件已验证,如何添加 ng-dbclick AngularJS?

javascript - 将数组转换为特定对象

javascript - javascript中的域和默认函数是什么意思?

javascript - Javascript 中类似数组的对象

javascript - 倒计时时钟 (JS)