A JavaScript Data filter (ii)--array filter __java

Source: Internet
Author: User
Tags sorts

The filter object constructed earlier can implement simple field filtering. However, in practical applications, we often encounter the need for the operation of the array, if only to search for simple arrays, delimitation, sorting is not complex, and when the array data mixed in more complex objects or the array contains complex objects, These needs will become more difficult.

Array filters are designed to deal with these problems, and we continue to start with a simple example:

The following is a very common array object:

[
	{' field1 ': 1, ' field2 ': 1, ' field3 ': 1, ' Field4 ': 4},
	{' field1 ': 1, ' Field2 ': 2, ' field3 ': 4, ' field4 ': 1},< c2/>{' field1 ': 1, ' Field2 ': 2, ' field3 ': 1, ' Field4 ': 2}, {' Field1 ': 2, ' field2 ': 1, ' field3 ': 2
	, ' Field4 ': 3},
  {' field1 ': 1, ' Field2 ': 2, ' field3 ': 2, ' Field4 ': 5}
]

Now for this array, our requirements are as follows:

Sorts the array based on field3 descending order. Only the first 2 items with the Field1 field value of 1 are preserved. Only the Field1 and field4 two fields are retained in the final filter results.

In view of the above requirements, in the normal way we will process the data in three steps, the estimated amount of code may need dozens of lines, when the requirements change, often the code messed up, OK, we use the idea of a filter to do look, the following constructs the following filter:

[
    ' field1 ',
    ' field4 ',
    [
        {
            ' $sort ': ['!field3 ']
        },
        {
            ' $filter ': {' field1 ': 1}
        },
        {
            ' $limit ': [0, 2]
        }
    ]
]

In the filter above, it is still the beginning of the field filter (each item in the outermost array of the filter represents the field to be preserved), and when the field filters the object that is an array, the filter automatically goes into effect for each item in the array.

Unlike the previous article, the third item of this filter is not a string that describes the field name, but an array, an array of so-called array filters .

Array Filters can currently contain three optional objects, namely $sort, $filter, and $limit. Where the $sort object implements an array of sorts, his value is a list of sorted fields, each item in the array represents a sort field, and a higher order priority is written on the previous field name, plus before the field name! character represents descending order (default is ascending).

$filter object implements a filter for the value of an array field (similar to where in SQL), whose value is an object in which each item in the object represents a filter condition, such as a filter that represents an item that selects the Field1 field value of 1. The current filter only supports exact matches, and subsequent joins of regular expression matching to perfect functionality.

The $limit object implements horizontal cropping of an array, which is a single or a number, where the first item of the array represents the offset of the crop, and the second represents the length of the crop. When a value is a number, it represents only the nth item of the array.

An array filter can be placed anywhere in the field filter, and the filter code will always have to finish processing the array filter before performing a field filtering action (vertical cropping).

The above filters will eventually produce the following results, consistent with what we need to anticipate:

[
	{' field1 ': 1, ' Field4 ': 1},
	{' field1 ': 1, ' Field4 ': 5}
]

Above three Array FiltersWith the introduction of the Field FilterAnd Child Field Filter, actually inadvertently we implemented a simple memory database (currently only supports lookup, not support updates).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.