How to use _.groupBy js for group object when the properties is in another object - TypeScript
up vote
2
down vote
favorite
I have the follow object
var cars = [
{
'make': 'audi',
'model': 'r8',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'audi',
'model': 'rs5',
'year': '2013',
location: {
'city': 'D',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'mustang',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'fusion',
'year': '2015',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'kia',
'model': 'optima',
'year': '2012',
location: {
'city': 'C',
'state': 'X',
'country': XX'
}
},
];
I would like group cars by city.
So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.
var groups = _.groupBy(cars, 'location.city');
could you please help me?
thanks
javascript angular typescript underscore.js
add a comment |
up vote
2
down vote
favorite
I have the follow object
var cars = [
{
'make': 'audi',
'model': 'r8',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'audi',
'model': 'rs5',
'year': '2013',
location: {
'city': 'D',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'mustang',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'fusion',
'year': '2015',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'kia',
'model': 'optima',
'year': '2012',
location: {
'city': 'C',
'state': 'X',
'country': XX'
}
},
];
I would like group cars by city.
So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.
var groups = _.groupBy(cars, 'location.city');
could you please help me?
thanks
javascript angular typescript underscore.js
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have the follow object
var cars = [
{
'make': 'audi',
'model': 'r8',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'audi',
'model': 'rs5',
'year': '2013',
location: {
'city': 'D',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'mustang',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'fusion',
'year': '2015',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'kia',
'model': 'optima',
'year': '2012',
location: {
'city': 'C',
'state': 'X',
'country': XX'
}
},
];
I would like group cars by city.
So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.
var groups = _.groupBy(cars, 'location.city');
could you please help me?
thanks
javascript angular typescript underscore.js
I have the follow object
var cars = [
{
'make': 'audi',
'model': 'r8',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'audi',
'model': 'rs5',
'year': '2013',
location: {
'city': 'D',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'mustang',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'fusion',
'year': '2015',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'kia',
'model': 'optima',
'year': '2012',
location: {
'city': 'C',
'state': 'X',
'country': XX'
}
},
];
I would like group cars by city.
So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.
var groups = _.groupBy(cars, 'location.city');
could you please help me?
thanks
javascript angular typescript underscore.js
javascript angular typescript underscore.js
edited Nov 21 at 16:13
asked Nov 21 at 14:28
Joh
547
547
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You could use _.property
with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.
_.property(path)
Returns a function that will return the specified property of any passed-in object.
path
may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
add a comment |
up vote
2
down vote
The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.
It's working for me:
let groups = _.groupBy(this.cars, car => car.location.city);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You could use _.property
with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.
_.property(path)
Returns a function that will return the specified property of any passed-in object.
path
may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
add a comment |
up vote
3
down vote
accepted
You could use _.property
with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.
_.property(path)
Returns a function that will return the specified property of any passed-in object.
path
may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You could use _.property
with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.
_.property(path)
Returns a function that will return the specified property of any passed-in object.
path
may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
You could use _.property
with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.
_.property(path)
Returns a function that will return the specified property of any passed-in object.
path
may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
groups = _.groupBy(cars, _.property(['location', 'city']));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
answered Nov 21 at 14:44
Nina Scholz
170k1383147
170k1383147
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
add a comment |
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
– Joh
Nov 21 at 15:51
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
i am sorry about typescript, but i have no idea of it.
– Nina Scholz
Nov 21 at 16:05
add a comment |
up vote
2
down vote
The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.
It's working for me:
let groups = _.groupBy(this.cars, car => car.location.city);
add a comment |
up vote
2
down vote
The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.
It's working for me:
let groups = _.groupBy(this.cars, car => car.location.city);
add a comment |
up vote
2
down vote
up vote
2
down vote
The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.
It's working for me:
let groups = _.groupBy(this.cars, car => car.location.city);
The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.
It's working for me:
let groups = _.groupBy(this.cars, car => car.location.city);
answered Nov 21 at 16:56
Joh
547
547
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414278%2fhow-to-use-groupby-js-for-group-object-when-the-properties-is-in-another-objec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown