So given a list of items like so:
item_1 = {id:1, categories: {"category_A" => 1, "category_B" => {"sub_category_A" => 3, "sub_category_B" => 1}}}
item_2 = {id:2, categories: {"category_B" => {"sub_category_A" => 1, "sub_category_B" => 2}}}
Where the numeric value is that items order in a given sub or main category. Now, given a sub or main category, I want to sort the items by the order number. In Ruby I'd write...
# Given category_B and sub_category_A
items.sort_by { |i| i.categories["category_B"]["sub_category_A"] }
# which would return...
[item_2, item_1]
Also want to add, the key is if an item does NOT have the relevant passed category_B and sub_category_A, it should be excluded entirely from output.
You don't need jQuery; JavaScript arrays have a filter()
function you can use to limit yourself to valid items and a sort()
function that can take a comparing function as its argument:
var item_1 = {
id:1,
categories: {
"category_A" : 1,
"category_B" : {
"sub_category_A" : 3,
"sub_category_B" : 1
}
}
};
var item_2 = {
id:2,
categories: {
"category_B" : {
"sub_category_A" : 1,
"sub_category_B" : 2
}
}
};
var item_3 = {
id: 3,
categories : {
"category_A" : 2
}
};
[item_1,item_2,item_3].filter(function(entry) {
return entry.categories.category_B;}).sort(function(left, right) {
return left.categories["category_B"]["sub_category_A"] -
right.categories["category_B"]["sub_category_A"]
});
// or in the more readable ES6 style
[item_1,item_2,item_3]
.filter((entry) => entry.categories.category_B)
.sort((left, right) => left.categories["category_B"]["sub_category_A"] - right.categories["category_B"]["sub_category_A"]
);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
You can use Array#Sort in JavaScript.
a.categories["category_B"]["sub_category_A"] - b.categories["category_B"]["sub_category_A"]
Is the comparison of 2 elements in Array.
UPDATE : to excluded from the output. You can use Array#filter it before sorting
Let's see my example
var arr = [{ id:1, categories: { "category_A" : 1, "category_B" : { "sub_category_A" : 3, "sub_category_B" : 1 } } },{ id:3, categories: { "category_C" : { "sub_category_A" : 1, "sub_category_B" : 2 } } },{ id:2, categories: { "category_B" : { "sub_category_A" : 1, "sub_category_B" : 2 } } }]; var result = arr.filter(a => a.categories["category_B"]) .sort((a, b) => { return a.categories["category_B"]["sub_category_A"] - b.categories["category_B"]["sub_category_A"]; }) console.log(result);
Neither ECMAScript nor jQuery have sortBy
, but LoDash does and so does Underscore .
It's also not hard to supply your own:
Array.prototype.sortBy = function sortBy(keyFn) { const compare = (a, b) => a > b ? 1 : (a < b ? -1 : 0); return this. map(el => [keyFn(el), el]). sort(([a, ], [b, ]) => compare(a, b)). map(([, el]) => el); }; const item1 = { id: 1, categories: { category_A: 1, category_B: { sub_category_A: 3, sub_category_B: 1 }}}; const item2 = { id: 2, categories: { category_B: { sub_category_A: 1, sub_category_B: 2 }}}; const sorted = [item1, item2].sortBy(el => el.categories.category_B.sub_category_A); console.log(sorted);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.