OrderBy with enumerable property in AngularJS - angularjs

I have an object array where every object can have an array of properties, like this
[
{
name: 'Big house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Small house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Green house',
properties: [{name: 'Area',value: '40'}, {name: 'Year',value: '2008'}]
},
{
name: 'Red house',
properties: [{name: 'Area',value: '250'}, {name: 'Year',value: '1999'}]
},
];
Now I'd like to order this list by one of the properties, say Area, using a filter in ng-repeat. Is that possible?
I've been play around with this in plunker (http://plnkr.co/edit/GEgLxv5zJyW0ZBTtJR5S?p=preview) but cannot figure out how to do.
Thanks in advance

This will work if the area is always the first property:
<tr ng-repeat="house in houses | orderBy:'properties[0].value'">
Also make sure that the area is defined as an integer, right now they are strings. So 250 will be sorted before 40.
So remove the quotes from the numbers:
{
name: 'Area',
value: 400
}
, {
name: 'Year',
value: '1950'
}

Related

Finding a property value in an array of object based on another property value

I have the following array
const arrayOfObject = [{
name: 'Mike',
age: '38',
}, {
name: 'Russell',
age: '42',
}, {
name: 'John',
age: '26',
}, ];
and I want to find the age of 'Russell' for instance.
If I use .find() I manage to get the whole object
const employeeAge = arrayOfObject.find((item) => item.name === 'Russell')
Is it possible to do it in one line?

Remove duplicates while creating an array with multiple properties in TypeScript

I receive an array from an API which looks as follows:
results = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
From this I'd like to create an array that looks like this:
countries = [
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'US'},
];
To this end, what I tried is:
countries = Array.from([...new Set(this.results.map(item => ({categoryOfFilter: 'country', value: item.country})))]);
Because I was told to use set. This does create an array as specified above, but it contains duplicates. Like so:
countries = [
{filter: 'country', value: 'US'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'US'},
];
Do you guys have any idea? The truth is that I was never any good with js in the first place so I'm waaay beyond stretching here.
The Set object lets you store unique values of any type, whether
primitive values or object references.
Sets don't work like that with objects. The items are all different, as they have different object references, even if their attributes all have equal values.
It will work like that (split it into two line to keep it a bit readable)
results = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
// create a set with all country codes. Set works fine with strings
const countryCodes = new Set(results.map(item => item.country));
// spread the set values into a new array and map that to the target objects
const countries = [...countryCodes].map(value => {return {filter: 'country', value}});
console.log(countries);
See this codepen: https://codepen.io/kyletanders/pen/NWqpWVX?editors=0012
something like this:
const data = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
let unique = [...new Set(data.map(item => item.country))].map(x => {return {filter: 'Country', value: x}});
console.log(unique);

AngularJS order by array of objects with nested values

I have an array of objects like this:
$scope.sortableItems =
[
{
name: 'Blue Shirt',
fields: {Price: 20}
},
{
name: 'Pink Shirt',
fields: {Price: 80},
},
{
name: 'Orange Shirt',
fields: {Price: 10},
}
]
I need to make an ng-repeat field that can sort the products based on price from low to high or high to low.
I tried this:
.product(ng-repeat="item in sortableItems | orderBy:fields.Price")
But it had no effect.
Additionally I have another variable $scope.sortFunction which can equal Price: Low-High or Price: High-Low the end result needs to detect the value of $scope.sortFunction and sort based on the value of the string. Not sure how to do that with an ng-repeat
You can create a function that would return price and use that function inside your orderBy
js
$scope.getPrice = function(item){
return item.fields['Price'];
}
html
<div ng-repeat="item in sortableItems | orderBy:getPrice:false">{{item.fields['Price']}}</div>
order by takes a second boolean value that you can use for asc / desc ordering.
Demo

How to group array of hashes by one key and merge inner keys and arrays

How can I transform an array of hashes:
[{
id: 115,
ref_id: 440000000000337,
properties: [{name: "test"}],
type: "content"
},{
id: 116,
ref_id: 440000000000337,
properties: [{name: "asdf"}],
type: "content"
}]
to get the desired result:
{
id: 440000000000337
type: "content"
properties: [{name: "test"}, {name: "asdf"}]
}
in one block smarter then [sic] in the example? Would it be best to obtain this [sic] results with ruby functions?
in = _
out = {properties: []}
in.map {|i| out[:id] = i[:ref_id]; out[:properties] << i[:properties]; out[:type] = i[:type]}
out[:properties].flatten!
You cannot use the variable name in since it is a keyword. I will use iin instead.
out = {
id: iin.last[:ref_id],
type: iin.last[:type],
properties: iin.flat_map{|e| e[:properties]}
}

Empty element in Angular ng-options (with and without grouping)

I have (a seemingly common) problem with empty option values an angular model viewed using a select using ng-options.
$scope.groupTypeOptions = [
{ group: 'g1', name: '---', value: null },
{ group: 'g2', name: 'Feature', value: 'feature' },
{ group: 'g2', name: 'Bug', value: 'bug' },
{ group: 'g2', name: 'Enhancement', value: 'enhancement' }
];
<select ng-model='form.groupType' required ng-options='option.value as option.name group by option.group for option in groupTypeOptions'></select>
A fiddle can be seen here:
http://jsfiddle.net/v6z3zh49/
My goal is to, from a predefined model, show a grouped select with the selected item representing null (or empty string). However, when selecting a value and I always end up with an extra, empty, option element being added. See fiddle above.
I have looked at similar questions, for example here and here, but cannot find a solution.
Any tips?
Try:
$scope.groupTypeOptions = [
{ group: 'g1', name: '---', value: undefined },
{ group: 'g2', name: 'Feature', value: 'feature' },
{ group: 'g2', name: 'Bug', value: 'bug' },
{ group: 'g2', name: 'Enhancement', value: 'enhancement' }
];
It happens because in javascript null !== undefined.
Hope, it will help.
Demo: http://jsfiddle.net/ababashka/skk4uj1y/

Resources