I am reading RSS feed and pushing both Title and Link into an Array in Jquery.
我正在阅读RSS提要,并使用Jquery将标题和链接推入一个数组。
What i did is
我所做的是
var arr = [];
$.getJSON("displayjson.php",function(data){
$.each(data.news, function(i,news){
var title = news.title;
var link = news.link;
arr.push({title : link});
});
});
And i am reading that array again using
我再次使用这个数组
$('#show').click(function(){
$.each(arr, function(index, value){
alert( index +' : '+value);
});
});
But it Giving me Output as
但它给出了输出。
1:[Object Object]
2:[Object Object]
3:[Object Object]
like this ...
像这样…
How i can get both tile and link as a pair ( title as key and link as value)
如何将平铺和链接作为一对(标题作为键,链接作为值)
5 个解决方案
#1
131
There are no keys in JavaScript arrays. Use objects for that purpose.
JavaScript数组中没有键。为此目的使用对象。
var obj = {};
$.getJSON("displayjson.php",function (data) {
$.each(data.news, function (i, news) {
obj[news.title] = news.link;
});
});
// later:
$.each(obj, function (index, value) {
alert( index + ' : ' + value );
});
In JavaScript, objects fulfill the role of associative arrays. Be aware that objects do not have a defined "sort order" when iterating them (see below).
在JavaScript中,对象履行关联数组的角色。请注意,在迭代对象时,对象没有定义的“排序顺序”(参见下面)。
However, In your case it is not really clear to me why you transfer data from the original object (data.news
) at all. Why do you not simply pass a reference to that object around?
但是,在您的情况下,我并不十分清楚为什么要从原始对象(data.news)传输数据。为什么不简单地向该对象传递一个引用呢?
You can combine objects and arrays to achieve predictable iteration and key/value behavior:
您可以组合对象和数组来实现可预测的迭代和键/值行为:
var arr = [];
$.getJSON("displayjson.php",function (data) {
$.each(data.news, function (i, news) {
arr.push({
title: news.title,
link: news.link
});
});
});
// later:
$.each(arr, function (index, value) {
alert( value.title + ' : ' + value.link );
});
#2
22
This code
这段代码
var title = news.title;
var link = news.link;
arr.push({title : link});
is not doing what you think it does. What gets pushed is a new object with a single member named "title" and with link
as the value ... the actual title
value is not used. To save an object with two fields you have to do something like
不是做你认为它做的事。被推送的是一个新对象,只有一个成员名为“title”,链接为值……不使用实际的标题值。要保存一个具有两个字段的对象,您必须执行以下操作
arr.push({title:title, link:link});
or with recent Javascript advances you can use the shortcut
或者使用最近的Javascript改进,您可以使用快捷方式
arr.push({title, link}); // Note: comma "," and not colon ":"
The closest thing to a python tuple would instead be
与python tuple最接近的是
arr.push([title, link]);
Once you have your objects or arrays in the arr
array you can get the values either as value.title
and value.link
or, in case of the pushed array version, as value[0]
, value[1]
.
一旦在arr数组中有了对象或数组,就可以将值作为值来获取。标题和值。如果是按下的数组版本,则为值[0],值[1]。
#3
14
I think you need to define an object and then push in array
我认为你需要定义一个对象然后推入数组
var obj = {};
obj[name] = val;
ary.push(obj);
#4
10
arr[title] = link;
You're not pushing into the array, you're setting the element with the key title
to the value link
. As such your array should be an object.
您没有进入数组,而是将元素设置为值链接的键标题。因此,数组应该是一个对象。
#5
2
You might mean this:
你可能会说:
var unEnumeratedArray = [];
var wtfObject = {
key : 'val',
0 : (undefined = 'Look, I\'m defined'),
'new' : 'keyword',
'{!}' : 'use bracket syntax',
' ': '8 spaces'
};
for(var key in wtfObject){
unEnumeratedArray[key] = wtfObject[key];
}
console.log('HAS KEYS PER VALUE NOW:', unEnumeratedArray, unEnumeratedArray[0],
unEnumeratedArray.key, unEnumeratedArray['new'],
unEnumeratedArray['{!}'], unEnumeratedArray[' ']);
You can set an enumerable for an Object like: ({})[0] = 'txt';
and you can set a key for an Array like: ([])['myKey'] = 'myVal';
您可以为如下对象设置一个可枚举值:({})[0]= 'txt';可以为数组设置一个键,比如:(])['myKey'] = 'myVal';
Hope this helps :)
希望这有助于:)
#1
131
There are no keys in JavaScript arrays. Use objects for that purpose.
JavaScript数组中没有键。为此目的使用对象。
var obj = {};
$.getJSON("displayjson.php",function (data) {
$.each(data.news, function (i, news) {
obj[news.title] = news.link;
});
});
// later:
$.each(obj, function (index, value) {
alert( index + ' : ' + value );
});
In JavaScript, objects fulfill the role of associative arrays. Be aware that objects do not have a defined "sort order" when iterating them (see below).
在JavaScript中,对象履行关联数组的角色。请注意,在迭代对象时,对象没有定义的“排序顺序”(参见下面)。
However, In your case it is not really clear to me why you transfer data from the original object (data.news
) at all. Why do you not simply pass a reference to that object around?
但是,在您的情况下,我并不十分清楚为什么要从原始对象(data.news)传输数据。为什么不简单地向该对象传递一个引用呢?
You can combine objects and arrays to achieve predictable iteration and key/value behavior:
您可以组合对象和数组来实现可预测的迭代和键/值行为:
var arr = [];
$.getJSON("displayjson.php",function (data) {
$.each(data.news, function (i, news) {
arr.push({
title: news.title,
link: news.link
});
});
});
// later:
$.each(arr, function (index, value) {
alert( value.title + ' : ' + value.link );
});
#2
22
This code
这段代码
var title = news.title;
var link = news.link;
arr.push({title : link});
is not doing what you think it does. What gets pushed is a new object with a single member named "title" and with link
as the value ... the actual title
value is not used. To save an object with two fields you have to do something like
不是做你认为它做的事。被推送的是一个新对象,只有一个成员名为“title”,链接为值……不使用实际的标题值。要保存一个具有两个字段的对象,您必须执行以下操作
arr.push({title:title, link:link});
or with recent Javascript advances you can use the shortcut
或者使用最近的Javascript改进,您可以使用快捷方式
arr.push({title, link}); // Note: comma "," and not colon ":"
The closest thing to a python tuple would instead be
与python tuple最接近的是
arr.push([title, link]);
Once you have your objects or arrays in the arr
array you can get the values either as value.title
and value.link
or, in case of the pushed array version, as value[0]
, value[1]
.
一旦在arr数组中有了对象或数组,就可以将值作为值来获取。标题和值。如果是按下的数组版本,则为值[0],值[1]。
#3
14
I think you need to define an object and then push in array
我认为你需要定义一个对象然后推入数组
var obj = {};
obj[name] = val;
ary.push(obj);
#4
10
arr[title] = link;
You're not pushing into the array, you're setting the element with the key title
to the value link
. As such your array should be an object.
您没有进入数组,而是将元素设置为值链接的键标题。因此,数组应该是一个对象。
#5
2
You might mean this:
你可能会说:
var unEnumeratedArray = [];
var wtfObject = {
key : 'val',
0 : (undefined = 'Look, I\'m defined'),
'new' : 'keyword',
'{!}' : 'use bracket syntax',
' ': '8 spaces'
};
for(var key in wtfObject){
unEnumeratedArray[key] = wtfObject[key];
}
console.log('HAS KEYS PER VALUE NOW:', unEnumeratedArray, unEnumeratedArray[0],
unEnumeratedArray.key, unEnumeratedArray['new'],
unEnumeratedArray['{!}'], unEnumeratedArray[' ']);
You can set an enumerable for an Object like: ({})[0] = 'txt';
and you can set a key for an Array like: ([])['myKey'] = 'myVal';
您可以为如下对象设置一个可枚举值:({})[0]= 'txt';可以为数组设置一个键,比如:(])['myKey'] = 'myVal';
Hope this helps :)
希望这有助于:)