Currently I have the following:
$scope.org = $.grep($scope.orgList, function (o) { return o.ServiceName ===
model.Org.ItemValue; })[0];
This will search orgList and extract only one value where the ItemValue is equal to model.Org.ItemValue
How can i do the following : I have 2 lists
$scope.tempOrgList which has values of
ItemValue = "Org1"
ItemValue = "Org2"
ItemValue = "Org8"
$scope.orgList has objects
ServiceName = "Org1", Name = "1"
ServiceName = "Org2" , Name = "2"
ServiceName = "Org3" ,Name = "3"
ServiceName = "Org4" , Name = "4"
ServiceName = "Org5" , Name = "5"
ServiceName = "Org6" , Name = "6"
ServiceName = "Org7" , Name = "7"
ServiceName = "Org8" , Name = "8"
ServiceName = "Org9" , Name = "9"
ServiceName = "Org10", Name = "10"
ServiceName = "Org11", Name = "11"
ServiceName = "Org12", Name = "12"
ServiceName = "Org13", Name = "13"
ServiceName = "Org14", Name = "14"
ServiceName = "Org15", Name = "15"
ServiceName = "Org16", Name = "16"
There are more fields for orgList but i have only shown 2 .
How can i only select the objects in orgList that have ServiceName equal to "Org1" and "Org2" and "Org3" and put them into an array , so basically what ever is in
tempOrgList, I want to select those from orgList where ServiceName in orglist is equal to the ItemValues in tempOrgList ?
CodePudding user response:
you can use a combination of array.filter
to get only elem that match a condition and array.some
to get only item present in tempOrgList where service name equal ItemValue
var newArray = $scope.orgList.filter(oneOrg => $scope.tempOrgList.some(oneTempOrg => oneTempOrg.ItemValue === oneOrg.ServiceName));
Following you will find the js equivalent to see the reply result
const tempOrgList = [
{ ItemValue: "Org1"},
{ ItemValue: "Org2"},
{ ItemValue: "Org8"}
]
const orgList = [
{ServiceName: "Org1", Name: "1"},
{ServiceName: "Org2" , Name: "2"},
{ServiceName: "Org3" ,Name: "3"},
{ServiceName: "Org4" , Name: "4"},
{ServiceName: "Org5" , Name: "5"},
{ServiceName: "Org6" , Name: "6"},
{ServiceName: "Org7" , Name: "7"},
{ServiceName: "Org8" , Name: "8"},
{ServiceName: "Org9" , Name: "9"},
{ServiceName: "Org10", Name: "10"},
{ServiceName: "Org11", Name: "11"},
{ServiceName: "Org12", Name: "12"},
{ServiceName: "Org13", Name: "13"},
{ServiceName: "Org14", Name: "14"},
{ServiceName: "Org15", Name: "15"},
{ServiceName: "Org16", Name: "16"}
];
var newArray = orgList.filter(oneOrg => tempOrgList.some(oneTempOrg => oneTempOrg.ItemValue === oneOrg.ServiceName));
console.log(newArray);