将$.每个结果存储到数组中
store $.each result into array?
我有$.each
获取Facebook好友列表。
这是$.each
代码
$.each(response.data, function(index,friend) {
alert(friend.name + ' has id:' + friend.id);
}
现在我如何将这些数据存储在数组中,使其像这个数组一样
friends = [
{ "name": "John" , "id": "1" },
{ "name": "Anna" , "id": "2" },
{ "name": "Peter" , "id": "3" }
];
$.map
类似于$.each
,但它返回函数的返回值数组:
var friends = $.map(response.data, function(index, friend) {
return {
name: friend.name,
id: friend.id
};
});
尽管…既然你似乎已经有了一个具有这些属性的对象数组,为什么不直接使用它呢?
尝试var friends = response.data;
。
您可能会得到额外的字段,但response.data已经是一个包含name
和id
字段的对象数组。它也可能包含其他字段。
因此,虽然您可以使用$.each
或$.map
处理response.data
以消除其他字段,但根据您的应用程序,可以按原样使用response.data
,而无需进一步处理。
相关文章:
- 将数组存储到本地存储中,而不是替换
- 如何将数组存储
- 在 laravel 4 上使用循环将数据数组存储到单个数组中
- 将对象/数组存储在 jQuery Cookie 中
- 想要将数组存储为 JavaScript 映射中的值
- 在 mysql 中使用多维数组存储 $_POST
- 将php多维数组存储在localstorage中
- 将数组存储在jQuery cookie中
- 如何将php数组存储在一个cookie中,然后通过javascript读取它
- 如何用单引号将数组存储在javascript中?有可能吗
- 将数组存储在html数据属性中
- 使用javascript将数组存储为json中的密钥对
- 将数组存储在函数内部与存储在对象内部有什么好处吗
- 试图使用javascript将Object.keys(obj)的返回数组存储在数组中,但得到错误“ReferenceEr
- 在javascript中使用数组存储一组具有固定值的变量
- 如何在localStorage (Javascript)中使用数组存储JSON
- 数组存储javascript
- 我如何从数组存储访问JS函数
- 关于如何使用数组存储其他变量名称的任何想法
- 在chartJs中使用动态数组存储标签和数据