Issue
I have a custom multi-select, when I select some options from it I need to activate a second multi-select, based on the response I receive from the options of first multi-select. So I have a method which takes in input an array of the selected options of first multi-select, and for each option I make a call to a rest, which sends me back the info to display in second multi-select. The code:
self.getDropdown = function (firstArray) {
firstArray.forEach(function (type) {
if (type.selectActive) {
$service
.getDropdownReq(self.info)
.then(function (respArray) {
if (secondArray.length) {
respArray.forEach(function (obj) {
secondArray.forEach(function (el) {
if (obj.name != el.name) {
secondArray = secondArray.concat(respArray)
}
})
})
} else if (!secondArray.length) {
secondArray = response
}
})
}
})
}
This is only one of the ways I tried but all unsuccessful.
Could I just "concat" the response from rest to the secondArray, check if the name value is equal and if so delete? Because so far all I get is a dropdown that keeps doubling each time I remove or add an item from first select. Note that all objects have same properties (so I would just need to check if the name property matches) like:
resp Array =
[{
id:1,
name: 'used Honda',
price: 'cheap'
}]
Also please note I can't use arrow functions.
Thanks to all!
Solution
You should filter the respArray to only include new answers before concatening it.
SecondArray is not defined in the scope you have provided, but it would be nice if it was defaulted to an empty array rather.
self.getDropdown = function (firstArray) {
firstArray.forEach(function (type) {
if (type.selectActive) {
$service.getDropdownReq(self.info).then(function (respArray) {
secondArray = secondArray || [];
secondArray = secondArray.concat(
respArray.filter(function (obj) {
return !secondArray.find((el) => el.name === obj.name);
})
);
});
}
});
};
Answered By - dhmmasson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.