I have a function that returns an array of objects data.sportdata. I would like to get all array elements with the same sports_id. The code
$scope.arrSportData = data.sportdata;
angular.forEach($scope.arrSportData, function(value, key) {
console.log($scope.arrSportData);
//getting reponse
/*
Object { id: "1", user_id: "2", sport_id: "1", position_id: "1", team_name: "JimmyTmname",}
Object { id: "2", user_id: "2", sport_id: "2", position_id: "6", team_name: "JimmyTmname2",}
Object { id: "3", user_id: "2", sport_id: "3", position_id: "12", team_name: "JimmyTmname3",}
Object { id: "4", user_id: "2", sport_id: "5", position_id: "20", team_name: "JimmyTmname5",}
*/
//code i wrote
if (value.sport_id == 1) {
$scope.positionId.spr1 = value.position_id;
$scope.teamname.spr1 = value.team_name;
}
if (value.sport_id == 2) {
$scope.positionId.spr2 = value.position_id;
$scope.teamname.spr2 = value.team_name;
}
if (value.sport_id == 3) {
$scope.positionId.spr3 = value.position_id;
$scope.teamname.spr3 = value.team_name;
}
if (value.sport_id == 4) {
$scope.positionId.spr4 = value.position_id;
$scope.teamname.spr4 = value.team_name;
}
});
Here I am always getting first value and nothing more. Please suggest and help to solve this problem.
I did try outside of the loop but does not work. I think filter function can do this but dont know how does it work.
Perform a groupBy function which will give you an object like this
{
key: [],
key: []
}
Here the key will be sport_id and [] will be the items with the same key.
A minimal working example will be,
// Written By: Ceaser Bautista
//Link: http://stackoverflow.com/a/34890276/17447
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
//here we have four teams with two sports id 13 and 22
var arrSportData = [{
id: "1",
user_id: "1",
sport_id: "13",
position_id: "1",
team_name: "JimmyTmname"
}, {
id: "2",
user_id: "2",
sport_id: "22",
position_id: "6",
team_name: "JimmyTmname2"
}, {
id: "3",
user_id: "3",
sport_id: "22",
position_id: "12",
team_name: "JimmyTmname2",
}, {
id: "4",
user_id: "4",
sport_id: "13",
position_id: "20",
team_name: "JimmyTmname1"
}];
$scope.groupedData = groupBy(arrSportData, "sport_id");
console.log(groupedData);
Now you will have an array for each sports_id. In view populate it like
<div ng-repeat="(key, items) in groupedData">
<h4>Sports ID: {{key}}</h4>
<ul ng-repeat="item in items">
<li>User ID: {{item.user_id}}</li>
</ul>
</div>
Related
Hey guys i have the following array:
Array [
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
I am going to take the users input in the form of a search bar, how can i sort the array based off the users input.
So lets say the user inputs
Bat g
the array would be sorted to:
Array [
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
How can I achieve this?
I have been searching around the array sort function:
Array.prototype.sort()
However I have only seen how to sort based off number comparisons I have never seen an array sorted based off string values like a search. Please could someone help me with this!
Here is function to search data using string text.
const searchItem = txt => {
let text = txt.toLowerCase();
let tracks = dataArray;
let filterTracks = tracks.filter(item => {
if (item.data.toLowerCase().match(text)) {
return item;
}
});
console.log('filterTracks', filterTracks);
};
Array Should be like this
var dataArray = [
{
data: 'Cat Man',
id: '1',
},
{
data: 'Bat Girl',
id: '2',
},
{
data: 'Mr Penguin',
id: '3',
},
{
data: 'Cheeky Cheetah',
id: '4',
},
];
How can I sort and group objects alphabetically by the first letter from an array in angular? I have seen the example to do this Sort and group objects alphabetically in Javascript and the exact answer and output json i am looking in Angular.
As of now My api json is like this stackblitz
Expexted api json would like this stackblitz
I have tried this but i am unable to found the solution in angular.
real Json:
employees = [
{ name: "Abigail", age: "25" },
{ name: "Axle", age: "29" },
{ name: "Brianna", age: "25" },
{ name: "Brooklyn", age: "23" },
{ name: "Camila", age: "24" },
{ name: "Charlotte", age: "28" },
{ name: "David", age: "22" }
];
expecting json after sort and group objects alphabetically by the first letter from an array would like:
[
{
"alphabet": "A",
"record": [
{ "name": "Abigail", "age": "25" },
{ "name": "Axle", "age": "29" }
]
},
{
"alphabet": "B",
"record": [
{ "name": "Brianna", "age": "25" },
{ "name": "Brooklyn", "age": "23" }
]
},
{
"alphabet": "C",
"record": [
{ "name": "Camila", "age": "24" },
{ "name": "Charlotte", "age": "28" }
]
},
{
"alphabet": "D", "record": [
{ "name": "David", "age": "22" }
]
}
]
expected output like:
A
Abigail
Axle
B
Brianna
Brooklyn
C
Camila
Charlotte
D
David
As mentioned in the comment, there is no Typescript specific way to sort and group the data. You could the JS Array#reduce to group the objects to your requirement.
Try the following
const employees = [ { name: "Abigail", age: "25" }, { name: "Axle", age: "29" }, { name: "Brianna", age: "25" }, { name: "Brooklyn", age: "23" }, { name: "Camila", age: "24" }, { name: "Charlotte", age: "28" }, { name: "David", age: "22" } ];
const output = employees
.reduce((acc, curr) => {
const idx = acc.findIndex(e => e.alphabet === curr.name[0]);
if (idx === -1) {
acc.push({ alphabet: curr.name[0], record: [curr] });
}
else {
acc[idx].record.push(curr);
acc[idx].record.sort((r1, r2) => r1.name > r2.name ? 1 : -1);
}
return acc;
}, [])
.sort((e1, e2) => e1.alphabet > e2.alphabet ? 1 : -1);
console.log(output);
It would look like following in the Stackblitz.
export class ExpansionOverviewExample {
#ViewChild(MatAccordion, { static: false }) accordion: MatAccordion;
employees = [
{ name: "Brianna", age: "25" },
{ name: "Axle", age: "29" },
{ name: "David", age: "22" },
{ name: "Brooklyn", age: "23" },
{ name: "Camila", age: "24" },
{ name: "Abigail", age: "25" },
{ name: "Charlotte", age: "28" }
];
constructor() {
this.employees = this.employees
.reduce((acc, curr) => {
const idx = acc.findIndex(e => e.alphabet === curr.name[0]);
if (idx === -1) {
acc.push({ alphabet: curr.name[0], record: [curr] });
} else {
acc[idx].record.push(curr);
acc[idx].record.sort((r1, r2) => (r1.name > r2.name ? 1 : -1));
}
return acc;
}, [])
.sort((e1, e2) => (e1.alphabet > e2.alphabet ? 1 : -1));
}
}
You could also use safe navigation operator ?. in the template so you don't get any undefined errors before the reduce is complete.
<div *ngFor="let mani of employees">
<div>
<p>{{mani?.alphabet}}</p>
<p *ngFor="let group of mani?.record"> {{ group?.name }}</p>
<hr>
</div>
</div>
I've updated your Stackblitz
I have a sorted array
let things = [
Thing(activity: "1", name: "value1"),
Thing(activity: "1", name: "value2"),
Thing(activity: "1", name: "value3"),
Thing(activity: "2", name: "value4"),
Thing(activity: "2", name: "value5"),
Thing(activity: "3", name: "value6"),
Thing(activity: "3", name: "value7"),
Thing(activity: "1", name: "value8"),
Thing(activity: "1", name: "value9"),
Thing(activity: "1", name: "value10")
]
I would like to produce array of arrays splitted when the activity value changes similar to the following
[[Thing(activity: "1", name: "value1"),
Thing(activity: "1", name: "value2"),
Thing(activity: "1", name: "value3")],
[Thing(activity: "2", name: "value4"),
Thing(activity: "2", name: "value5")],
[Thing(activity: "3", name: "value6"),
Thing(activity: "3", name: "value7")],
[Thing(activity: "1", name: "value8"),
Thing(activity: "1", name: "value9"),
Thing(activity: "1", name: "value10")]]
A generalized solution would be:
extension Sequence {
func grouped<T: Equatable>(by block: (Element) throws -> T) rethrows -> [[Element]] {
return try reduce(into: []) { result, element in
if let lastElement = result.last?.last, try block(lastElement) == block(element) {
result[result.index(before: result.endIndex)].append(element)
} else {
result.append([element])
}
}
}
}
Then you can do:
let results = things.grouped { $0.activity }
A less elegant (but slightly more efficient) solution would be:
extension Sequence {
func grouped<T: Equatable>(by block: (Element) throws -> T) rethrows -> [[Element]] {
var results: [[Element]] = []
var lastValue: T?
var index = results.endIndex
for element in self {
let value = try block(element)
if let lastValue = lastValue, lastValue == value {
results[index].append(element)
} else {
results.append([element])
index = results.index(before: results.endIndex)
lastValue = value
}
}
return results
}
}
As already mentioned by #matt in comments you can use collection method reduce(into:) to group your elements by checking if the activity of the last element of the last array is equal to the current element activity, if so just append a new element to the last array, otherwise append a new array with a single element to the outer array:
struct Thing {
let activity, name: String
}
let things: [Thing] = [
.init(activity: "1", name: "value1"),
.init(activity: "1", name: "value2"),
.init(activity: "1", name: "value3"),
.init(activity: "2", name: "value4"),
.init(activity: "2", name: "value5"),
.init(activity: "3", name: "value6"),
.init(activity: "3", name: "value7"),
.init(activity: "1", name: "value8"),
.init(activity: "1", name: "value9"),
.init(activity: "1", name: "value10")]
let grouped: [[Thing]] = things.reduce(into: []) {
$0.last?.last?.activity == $1.activity ?
$0[$0.index(before: $0.endIndex)].append($1) :
$0.append([$1])
}
print(grouped) // "[[__lldb_expr_1.Thing(activity: "1", name: "value1"), __lldb_expr_1.Thing(activity: "1", name: "value2"), __lldb_expr_1.Thing(activity: "1", name: "value3")], [__lldb_expr_1.Thing(activity: "2", name: "value4"), __lldb_expr_1.Thing(activity: "2", name: "value5")], [__lldb_expr_1.Thing(activity: "3", name: "value6"), __lldb_expr_1.Thing(activity: "3", name: "value7")], [__lldb_expr_1.Thing(activity: "1", name: "value8"), __lldb_expr_1.Thing(activity: "1", name: "value9"), __lldb_expr_1.Thing(activity: "1", name: "value10")]]\n"
I'm trying to create a function that will return the needed attribute from an object.
The object will look like this:
export var Characters = [
{
id: 1,
Name: "Abe",
HitPointValue: "124",
StrengthValue: "12",
IntelligenceValue: "14",
WisdomValue: "16",
DexterityValue: "12",
ConstitutionValue: "10",
CharismaValue: "17",
Avatar: require('./images/avatar_7.jpg')
}
]
I tried this:
export function getStat(id, stat) {
var idx = Characters.findIndex((val) => val.id == id);
return Characters[idx].stat;
}
For example, let's say I need to get the "WisdomValue" of this object.
So I call it like this:
<Text style={[styles.stats]}>
{"\n"}
Wisdom Value: {getStat(1, 'WisdomValue')}{"\n"}
</Text>
But I just get an error 'undefined is not an object'
How can I get just one specific attribute, but in a dynamic way? So I don't have to write a separte function like, getHitPointValue(id), get StrengthValue(id), etc...
thanks!
Use [] instead of using dot notation, because you are trying to access a value by a dynamic key.
Check this snippet:
var Characters = [
{
id: 1,
Name: "Abe",
HitPointValue: "124",
StrengthValue: "12",
IntelligenceValue: "14",
WisdomValue: "16",
DexterityValue: "12",
ConstitutionValue: "10",
CharismaValue: "17",
}
]
function getStat(id, stat) {
var idx = Characters.findIndex((val) => val.id == id);
if(idx >= 0)
return Characters[idx][stat];
else return "not found"
}
console.log(getStat(1, 'WisdomValue'));
console.log(getStat('15', 'abc'));
var Characters = [
{
id: 1,
Name: "Abe",
HitPointValue: "124",
StrengthValue: "12",
IntelligenceValue: "14",
WisdomValue: "16",
DexterityValue: "12",
ConstitutionValue: "10",
CharismaValue: "17",
}
]
function getStat(id, stat) {
// you can directly find the object rather going through the array index
var character = Characters.find((val) => val.id == id);
// care for non existing characters
if (!character) {
throw new Error(`Character with id ${id} does not exist`);
}
// care for non existing stats
if (!character.hasOwnProperty(stat)) {
throw new Error(`Stat ${stat} is not available for character with id ${id}`);
}
// use the [] notation as opposed to the dot notation when evaluating dynamic property names
return character[stat];
}
console.log(`Wisdom Value: ${getStat(1, 'WisdomValue')}`);
console.log(`Charisma Value: ${getStat(1, 'CharismaValue')}`);
try {
console.log(getStat(2, 'Name'));
} catch (e) {
console.log(e.message);
}
try {
console.log(getStat(1, 'PersuasionValue'));
} catch (e) {
console.log(e.message);
}
This question already has answers here:
Sorting array alphabetically with number
(8 answers)
Closed 5 years ago.
I have an array of strings,
let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
I would like to get the output sorted in ascending as,
let sorted = [ "1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA" ]
I tried using the sorted command but it does not work when it encounters more than 2 digits e.g.: 100, 101, 200 etc.
array.sorted { $0? < $1? }
What would be the simple way to get this?
Xcode 11.3 • Swift 5.2 or later
You can use String method localizedStandardCompare (diacritics and case insensitive):
let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
let sorted = array.sorted {$0.localizedStandardCompare($1) == .orderedAscending}
print(sorted) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]
or using the method sort(by:) on a MutableCollection:
var array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
array.sort {$0.localizedStandardCompare($1) == .orderedAscending}
print(array) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]
You can also implement your own localized standard sort method extending Collection:
public extension Sequence where Element: StringProtocol {
func localizedStandardSorted(ascending: Bool = true) -> [Element] {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sorted { $0.localizedStandardCompare($1) == result }
}
}
let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
let sorted = array.localizedStandardSorted()
print(sorted) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]
The mutating method as well extending MutableCollection:
public extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
mutating func localizedStandardSort(ascending: Bool = true) {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sort { $0.localizedStandardCompare($1) == result }
}
}
var array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
array.localizedStandardSort()
print(array) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]
If you need to sort your array numerically you can use String compare method setting the options parameter to .numeric:
public extension Sequence where Element: StringProtocol {
func sortedNumerically(ascending: Bool = true) -> [Element] {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sorted { $0.compare($1, options: .numeric) == result }
}
}
public extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
mutating func sortNumerically(ascending: Bool = true) {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sort { $0.compare($1, options: .numeric) == result }
}
}
var numbers = ["1.5","0.5","1"]
let sortedNumbers = numbers.sortedNumerically()
print(sortedNumbers) // ["0.5", "1", "1.5"]
print(numbers) // ["1.5","0.5","1"]
// mutating the original collection
numbers.sortNumerically(ascending: false)
print(numbers) // "["1.5", "1", "0.5"]\n"
To sort a custom class/structure by one of its properties:
extension MutableCollection where Self: RandomAccessCollection {
public mutating func localizedStandardSort<T: StringProtocol>(_ predicate: (Element) -> T, ascending: Bool = true) {
sort {
predicate($0).localizedStandardCompare(predicate($1)) ==
(ascending ? .orderedAscending : .orderedDescending)
}
}
}
public extension Sequence {
func localizedStandardSorted<T: StringProtocol>(_ predicate: (Element) -> T, ascending: Bool = true) -> [Element] {
sorted {
predicate($0).localizedStandardCompare(predicate($1)) ==
(ascending ? .orderedAscending : .orderedDescending)
}
}
}
public extension Sequence {
func sortedNumerically<T: StringProtocol>(_ predicate: (Element) -> T, ascending: Bool = true) -> [Element] {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sorted { predicate($0).compare(predicate($1), options: .numeric) == result }
}
}
public extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
mutating func sortNumerically<T: StringProtocol>(_ predicate: (Element) -> T, ascending: Bool = true) {
let result: ComparisonResult = ascending ? .orderedAscending : .orderedDescending
return sort { predicate($0).compare(predicate($1), options: .numeric) == result }
}
}
Playground testing
struct Person {
let name: String
let age : Int
}
extension Person : CustomStringConvertible {
var description: String { "name: \(name), age: \(age)" }
}
var people: [Person] = [.init(name: "Éd Sheeran", age: 26),
.init(name: "phil Collins", age: 66),
.init(name: "Shakira", age: 40),
.init(name: "rihanna", age: 25),
.init(name: "Bono", age: 57)]
let sorted = people.localizedStandardSorted(\.name)
print(sorted) // [name: Bono, age: 57, name: Éd Sheeran, age: 26, name: phil Collins, age: 66, name: rihanna, age: 25, name: Shakira, age: 40]
edit/update: Xcode 12 • Swift 5.5 or later
You can use KeyPathComparator and pass localizedStandard as the Comparator:
people.sort(using: KeyPathComparator(\.name, comparator: .localizedStandard))
print(people) // [name: Bono, age: 57, name: Éd Sheeran, age: 26, name: phil Collins, age: 66, name: rihanna, age: 25, name: Shakira, age: 40]
people.sort(using: KeyPathComparator(\.name, comparator: .localizedStandard, order: .reverse))
print(people) // "[name: Shakira, age: 40, name: rihanna, age: 25, name: phil Collins, age: 66, name: Éd Sheeran, age: 26, name: Bono, age: 57]"
For sorting just an array of strings you can also use KeyPathComparator and pass self for the KeyPath:
var array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
array.sort(using: KeyPathComparator(\.self, comparator: .localizedStandard))
array // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]
array.sort(using: KeyPathComparator(\.self, comparator: .localizedStandard, order: .reverse))
array // ["NA", "NA", "210", "200", "101", "100", "20", "10", "7", "1"]