如何将数据推送到JavaScript对象文本
我想用JavaScript对象文字推送数据。但是,无论如何也找不到。我正在尝试以下代码如何将数据推送到JavaScript对象文本,javascript,angularjs,javascript-objects,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: weath
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
}
]
});
第二行代码用普通对象覆盖
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
}
]
});
第二行代码用普通对象覆盖
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
}
]
});
第二行代码用普通对象覆盖
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
}
]
});
嗯,这与问题不太相关,但是为什么不创建一个临时变量并将
weatherData.query.results.weather.rss.channel.item
存储在其中呢?因为这个问题与JSON完全无关,所以我删除了与问题无关的JSON
tag.um,但是为什么不创建一个临时变量并将weatherData.query.results.weather.rss.channel.item
存储在其中呢?因为这个问题与JSON完全无关,所以我删除了与问题无关的JSON
tag.um,但是为什么不创建一个临时变量并将weatherData.query.results.weather.rss.channel.item
存储在其中呢?因为这个问题与JSON完全无关,所以我删除了与问题无关的JSON
tag.um,但是为什么不创建一个临时变量并将weatherData.query.results.weather.rss.channel.item
存储在其中呢?因为这个问题与JSON完全无关,所以我删除了JSON
标记。