如何将键和值都推送到 Jquery 中的数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4825899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to push both key and value into an Array in Jquery
提问by KillerFish
I am reading RSS feedand pushing both Title and Link into an Array in Jquery.
我正在阅读RSS 提要并将 Title 和 Link 都推送到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 Outputas
但它给我的输出为
1:[Object Object]
2:[Object Object]
3:[Object Object]
like this ...
像这样 ...
How i can get both tileand linkas a pair ( title as key and link as value)
我如何将磁贴和链接作为一对(标题作为键和链接作为值)
回答by Tomalak
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 thatobject around?
但是,在您的情况下,我不太清楚您为什么要从原始对象 ( data.news
)传输数据。为什么不简单地传递对该对象的引用?
You can combine objects and arrays to achieve predictable iteration andkey/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 );
});
回答by 6502
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”的成员,并且link
作为值......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 元组的东西是
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
您就可以获取值 asvalue.title
和value.link
或者,如果是推送的数组版本,则为 as value[0]
, value[1]
。
回答by deceze
arr[title] = link;
You're not pushinginto the array, you're setting the element with the key title
to the value link
. As such your array should be an object.
您没有推入数组,而是将键title
设置为 value的元素link
。因此,您的数组应该是一个对象。
回答by Jaydeep Patel
I think you need to define an object and then push in array
我认为你需要定义一个对象然后推入数组
var obj = {};
obj[name] = val;
ary.push(obj);
回答by Cody
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';
您可以为 Object 设置一个 enumerable like:({})[0] = 'txt';
并且您可以为 Array 设置一个键,例如:([])['myKey'] = 'myVal';
Hope this helps :)
希望这可以帮助 :)