I want to create new array of object from apiResponse which will contain all the areaCode
for particular RouteId
and InDirection
.
apiResponse = [
{
"RouteId": "1",
"InDirection": "1",
"AreaCode": "41108",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "1",
"AreaCode": "41109",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "1",
"AreaCode": "41110",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "1",
"AreaCode": "41111",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "2",
"AreaCode": "41108",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "2",
"AreaCode": "41109",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "1",
"InDirection": "2",
"AreaCode": "411011",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "2",
"InDirection": "1",
"AreaCode": "41112",
"LastModified": "2011-06-15 00:00:00.000"
},
{
"RouteId": "2",
"InDirection": "1",
"AreaCode": "41114",
"LastModified": "2011-06-15 00:00:00.000"
}]
For e.g. for RouteId = 1 and InDirection = 1, result should be
result = [{
"RouteId": "1",
"InDirection": "1",
"AreaCode": ["41108", "41109", "41110", "41111"],
}]
and the final result for the given apiResponse should be
finalResult = [{
"RouteId": "1",
"InDirection": "1",
"AreaCode": ["41108", "41109", "41110", "41111"],
},
{
"RouteId": "1",
"InDirection": "2",
"AreaCode": ["41108", "41109", "411011"],
}
{
"RouteId": "2",
"InDirection": "1",
"AreaCode": ["41112", "41114"],
}]
I tried to extract InDirection but couldn’t create desired array
apiResponse.filter(x => x.RouteId === '1' && x.InDirection === '1').map(x => x.AreaCode);
Can someone help me with this?
2
Answers
Use reduce to get the final result.