我如何“分组”在javascript中 [英] How do I "group" in javascript

查看:63
本文介绍了我如何“分组”在javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





小组不是正确的词,但它确实会影响我需要的结果。



我将有一个包含Category的对象数组。

我需要创建一个新的数组(列表?)对象,如下所示:



{类别:详情,项目:[{etc ...}]}。



我该怎么办?



谢谢^ _ ^



PS:这是我的基础对象构造函数,可以给你一个想法:

Hi,

Group isn't the right word, but it does effect the result I need.

I will have an array of objects that include "Category".
I need to create a new array (list?) of objects that look like this:

{Category: "Details", Items: [{ etc...}] }.

How do I do this?

Thanks ^_^

PS: this is my base object constructor to give you an idea:

var Change = function (category, item, from, to, requiresAuth) {
    this.Category = category;
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = "alert-danger";
    else
        this.Severity = "alert-warning";
}







编辑:我没有很好地解释我的要求。



我有一系列Change()类型。

我需要将其转换为按类别分组的数组。更像是这样:






I did not explain my requirements well.

I have an array of Change() types.
I need to transform that into an array grouped by Category. More like this:

var Change = function (category) {
    this.Category = category;
    this.Items = []
}
var Item = function(item, from, to, requiresAuth){
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = "alert-danger";
    else
        this.Severity = "alert-warning";
}





我使用JSON语法作为上述的简写。很抱歉没有说清楚



I used JSON syntax as shorthand for the above. Sorry for not making that clear

推荐答案

首先要了解的是:使用JavaScript,您不需要列表或其他集合。只有一个集合,一个JavaScript对象(类型为'object'),它有一个专门的表单,数组,只有一个区别:它具有属性 length 。所有内容都被组织为键值对,关联数组的元素,它为按键搜索提供了O(1)的时间复杂度

https://en.wikipedia.org/wiki/JavaScript#Dynamic [ ^ ],

https://en.wikipedia.org/wiki/Associative_array [ ^ ],

https ://en.wikipedia.org/wiki/Big_O_notation [ ^ ],

https://en.wikipedia.org/wiki/Time_complexity [ ^ ]。



这是常规对象:

First thing to understand: with JavaScript, you don't need lists or other collections. There is only one collection, a JavaScript object (of type 'object'), which has one specialized form, array, with only one difference: it has the property length. Everything is organized as key-value pairs, elements of an associative array, which gives your the time complexity of O(1) for search-by-key:
https://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
https://en.wikipedia.org/wiki/Associative_array[^],
https://en.wikipedia.org/wiki/Big_O_notation[^],
https://en.wikipedia.org/wiki/Time_complexity[^].

This is the "regular" object:
var object = {a:1, b:"some value"};



数组的唯一区别是具有整数索引属性,这些属性在属性 length 下自动计算,例如:


The only difference for array is that is has integer-indexed properties which are automatically counted under the property length, for example:

var array = ["a", "b", "c"];
array.someProperty = 12323;
array[13] = 12;



值得注意的是,您始终可以向任何对象(数组,函数,任何对象)添加常规属性( someProperty ;在此示例中)。此示例中的长度为14.除分配之外的任何整数索引下的所有索引值均为 undefined



剩下的部分是使用 new 。如果使用 new 调用函数,它将从原型返回一些对象(即使您没有原型,也使用了一些默认原型对象),并且克隆了返回原型,并通过 .this 添加您分配的所有属性。



制作和示例你的内容想要,我需要知道你想要在物品中有什么。我不知道,您可以自己考虑一下,比如说,稍后添加这些项目。另外,我想解决你的大问题:硬编码的立即常量,如alert-warning。所以,您可以:


Notably, you can always add a "regular" property (someProperty; in this example) to any object (array, function, anything). The length in this example is 14. All the indexed values under any integer indices except assigned are undefined.

The remaining piece is using new. If a function is called with new, it returns some object from prototype (even if you don't have a prototype, some default prototype object is used), and the clone of prototype is returned, with all properties you assign through .this added.

To make and example of what you want, I would need to know what do you want to have in items. As I have no idea, you can think about it yourself, say, add the items later. Also, I want to fix your big problem: hard-coded immediate constants like "alert-warning". So, you can have:

// analog of enum:
var Severity = {alertDanger:1, alertWarning:0}

var Change = function (category, items) {
    this.Category = category;
    if (!items)
       this.Items = []
}

var Item = function(item, from, to, requiresAuth) {
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = Severity.alertDanger;
    else
        this.Severity = Severity.alertWarning;
}

var itemsArray = [new Item("item1", 1, 3), new Item("item 2", 3, 4, true)]
var firstChange = new Change("NoItems");
var secondChange = new Change("Details", itemsArray);





这在您澄清问题后修改了代码示例。如果不清楚,请随时询问您的后续问题。



使用数组(您可能不需要),请参阅第一个代码示例和我的解释。

对于使用原型和原型链,请参阅我最近的回答:那里发生了什么与原型有关? [ ^ ]。



参见:

https://developer.mozilla.org/en-US/docs/ Web / JavaScript / Guide / Working_with_Objects [ ^ ],

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object [< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objecttarget =_ blanktitle =New Window> ^ ]。< br $>


-SA



This code sample was modified after your clarifications of the question. Feel free to ask your follow-up questions if something is not clear.

For working with arrays (which you may not need), see the first code sample and my explanations.
For working with prototypes and prototype chains, please see my recent answer: What is happening there related with prototypes?[^].

See also:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[^].

—SA


这篇关于我如何“分组”在javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆