English 中文(简体)
一组财产
原标题:Group javascript items by one property
最佳回答

很难告诉你所需要的确切结构。

该法典:

       // Split values into arrays
Code = Code.split( * );
Name = Name.split( * );
ids = ids.split( * );

       // cache the length of one and create the result object
var length = Code.length;
var result = {};

       // Iterate over each array item
       // If we come across a new code, 
       //    add it to result with an empty array
for(var i = 0; i < length; i++) {
    if(Code[i] in result == false) {
        result[ Code[i] ] = [];
    }
            // Push a new object into the Code at "i" with the Name and ID at "i"
    result[ Code[i] ].push({ name:Name[i], id:ids[i] });
}

形成这一结构:

// Resulting object
{
      // A12 has array with one object
    A12: [ {id: "1", name: "John"} ],

      // B22 has array with two objects
    B22: [ {id: "2", name: "Brain"},
           {id: "3", name: "Andy"}
         ]
}
问题回答
  1. Split the strings on "*" so that you have 3 arrays.
  2. Build objects from like-indexed elements of each array.
  3. While building those objects, collect a second object that contains arrays for each "Code" value.

法典:

function toGroups(ids, names, codes) {
  ids = ids.split( * );
  names = names.split( * );
  codes = codes.split( * );
  if (ids.length !== names.length || ids.length !== codes.length)
    throw "Invalid strings";

  var objects = [], groupMap = {};
  for (var i = 0; i < ids.length; ++i) {
    var o = { id: ids[i], name: names[i], code: code[i] };
    objects.push(o);
    if (groupMap[o.code]) {
      groupMap[o.code].push(o);
    else
      groupMap[o.code] = [o];
  }
  return { objects: objects, groupMap: groupMap };
}

你们说,“两阵”是你想要的,是该功能归还的物体的“集体”财产。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签