Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数组转换为对象?_Javascript_Jquery_Arrays_Object - Fatal编程技术网

Javascript 将数组转换为对象?

Javascript 将数组转换为对象?,javascript,jquery,arrays,object,Javascript,Jquery,Arrays,Object,我有一个包含多个对象的数组: var getUsers = function (){ allUsers = []; $().SPServices({ operation: "GetUserCollectionFromSite", completefunc: function(xData, Status){ var responseXML = $(xData.responseXML); responseXML.find(

我有一个包含多个对象的数组:

  var getUsers = function (){
    allUsers = [];

    $().SPServices({
      operation: "GetUserCollectionFromSite",
      completefunc: function(xData, Status){
        var responseXML = $(xData.responseXML);
        responseXML.find("User").each(function(){
          allUsers.push({
            id: $(this).attr("ID"),
            name: $(this).attr("Name"),
            domain: $(this).attr("LoginName"),
            email: $(this).attr("Email"),
            isAdmin: $(this).attr("IsSiteAdmin")
          });
        });
      }
    });
    return allUsers;
  }
我正在尝试更改它,这样就不用使用数组,
allUsers
而是一个对象,顶级属性是ID,每个ID属性都包含用户信息

这是我尝试过的,但它有点导致我的应用程序由于某种原因而暂停

var getUsers = function (){
      allUsers = {};

    $().SPServices({
      operation: "GetUserCollectionFromSite",
      completefunc: function(xData, Status){
        var responseXML = $(xData.responseXML);
        responseXML.find("User").each(function(){
            //This is pretty much where I'm at a lost
          allUsers[$(this).attr("ID")]:{
            name: $(this).attr("Name"),
            domain: $(this).attr("LoginName"),
            email: $(this).attr("Email"),
            isAdmin: $(this).attr("IsSiteAdmin")
          };
        });
      }
    });
    return allUsers;
  } 
我试图得到一个结构如下的对象:

var allUsers = {
    "68": {
        id: 68,
        name: 'mike',
        domain: 'i: 0#.f | admembers | mike.ca',
        email: 'mike.ca',
        isAdmin: false
    }
};

PS:很抱歉标题不好。

只需将
替换为
=
,在
allUsers[$(this).attr(“ID”):{

  allUsers[$(this).attr("ID")] = {
    id: $(this).attr("ID"),
    name: $(this).attr("Name"),
    domain: $(this).attr("LoginName"),
    email: $(this).attr("Email"),
    isAdmin: $(this).attr("IsSiteAdmin")
  };
这里有一个输入错误:

allUsers[$(this).attr("ID")] : {
                             ^
应该是
=
,因为这是对键的赋值

allUsers[$(this).attr("ID")] = {
其他一切看起来都很好。除了您可以将
$(this)
分配给一个变量,以避免进行DOM查找

var self = $(this);

this.id
而不是
$(this.attr('id'))
可能更好?@qwertnl是否知道它正在从当前xml节点查找id属性?工作正常,谢谢。我尝试在函数外部,在我的服务中设置var self,但由于某些原因没有工作。除此之外,似乎工作正常。是的,我注意到了这一点。我故意尝试不将其放入其中,因此至少,它不必经常声明它,但我想它在其他情况下不会工作。@Batman这是因为
this
需要引用正在迭代的元素,这是该函数的作用域。