How can do array map inside methods in vuejs - arrays

How can ı equalize my array ıd and my value ıd and access value.name I didn't do it
This is my code:
activity(val) {
var act = this.items.map(function (val) {
if (element.ActivityID== val) {
return element.ActivityName
}
return act
});

Perhaps this?
activity (val) {
const activity = this.items.find(item => item.ActivityID === val)
return activity && activity.ActivityName
}
This just finds the item with the corresponding ActivityID and then returns its ActivityName.
Your original code contained several possible mistakes:
Two different things called val.
element doesn't appear to be defined.
The return act was inside the map callback. The activity method itself wasn't returning anything.
Not really clear why you were using map to find a single item. map is used to create a new array with the same length as the original array with each item in the new array determined by the equivalent item in the original array. It 'maps' the items of the input array to the items in the output array.

Related

create a new Array with filtered out props

I have been trying to filter an Array by its props and not by its value so my original array would be -
const orignalArray =[
{id: 1, name:"jim", email:"jim#mail.com",age:20},
{id: 1, name:"jom", email:"jom#mail.com",age:30}
]
id like to be able to use (n) amount of filters.
My output array would look ideally look like this
const filterList["id","age"]
const newArray=[{name:"jim", email:"jim#mail.com"},{ name:"jom", email:"jom#mail.com"}]
I have tried to use filter() but cant seem to get it to work.
any help is much appreciated.
In this case you aren't filtering the array rather creating a new one based on the original with derived elements from each. To achieve this you can use the array map function to loop over the array and create a new one with new objects without the desired properties, e.g.:
function removeArrayElementProps(arr, propsToRemove) {
return arr.map((element) => {
// Create a copy of the original element to avoid modifying the original
const newElement = {...element};
// Remove the desired properties from the new element
propsToRemove.forEach((propToRemove) => {
delete newElement[propToRemove];
});
// Returning the modified element in map puts it in thew new arry
return newElement;
});
}
Then you simply call:
const newArr = removeArrayElementProps(orignalArray, ['id','age']);
This loops over the array, creates a copy of each element, and removes the specified properties from the element.

Swift array filter with empty filter value

I am filtering an array based on a set filter supplied to the view (this filter varies) but want to modify the function to allow for an empty filter, i.e return the original array un filtered.
I feel like this should be a simple matter but I'm currently a bit lost - have looked at the .isEmpty modifier and a couple of different IF statements but I'm not "getting it"
var categoryFilter:String
var filteredCategoryTasks: [Task] {
modelData.tasks.filter { task in
(task.category == categoryFilter)
}
}
You could return the original array in the event that the String is empty and the filtered array otherwise:
var filteredCategoryTasks: [Task] {
categoryFilter.isEmpty ? modelData.tasks : modelData.tasks.filter { task in
(task.category == categoryFilter)
}
}

Problem Using _.compact lodash to eliminate null values on an array of objects

I'm manipulating an array of objects that i get from an http request containing coordinates to create markers in google-maps , but i need to eliminate all the null values in the array. I'm trying with compact but it gives back the same array unchanged.
//this is the resulting array structure
var array=
[{"id":0,"latitude":45.17850875854492,"longitude":7.773523330688477},{"id":1,"latitude":45.122344970703125,"longitude":7.7135162353515625},{"id":2,"latitude":null,"longitude":null},{"id":3,"latitude":45.11630630493164,"longitude":7.730717658996582},{"id":4,"latitude":45.116214752197266,"longitude":7.730687141418457},{"id":5,"latitude":null,"longitude":null}]
var comp =_.compact(array)
i dont get any error in the cosole , but the variable comp return the same exact array without removing null values
All your values are arrays, and the null is a value of your properties. The _.compact() method works with primitives.
Use _.reject() and check with _.isNull if the properties are null, and the object should be removed:
const array =
[{"id":0,"latitude":45.17850875854492,"longitude":7.773523330688477},{"id":1,"latitude":45.122344970703125,"longitude":7.7135162353515625},{"id":2,"latitude":null,"longitude":null},{"id":3,"latitude":45.11630630493164,"longitude":7.730717658996582},{"id":4,"latitude":45.116214752197266,"longitude":7.730687141418457},{"id":5,"latitude":null,"longitude":null}]
const result = _.reject(array, ({ latitude, longitude }) =>
_.isNull(latitude) || _.isNull(longitude)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
You can use _.pickBy()
Creates an object composed of the object properties predicate returns truthy for
lodash
This applies on object so for an array you can use that:
var comp = _.map(array, item => _.pickBy(item));

Extjs4, filter store dynamically

I fill out an array dynamically with a list of Ids:
var allChildNodeIDs = [];
Ext.getCmp('categoriesTreePanel').getRootNode().eachChild(function(Mynode){
allChildNodeIDs.push(Mynode.data.idCategorie);
});
Then I want to filter a store according to all Ids contained in the array. For example if the array contains two values, I want that my store will be filtered like so:
myStore.filterBy(function(record) {
return (record.get('idCategorie') == allChildNodeIDs [1] && record.get('idCategorie') == allChildNodeIDs [2]);
});
But the array is filled out dynamically, and I don't know his length!
It seems like the sorting function in your example would always return false. I assume what you want to do is accept all the record which category is present into the array (i.e. that ou meant to use || instead of &&).
You can use Array's indexOf method.
Example:
myStore.filterBy(function(record) {
return allChildNodeIDs.indexOf(record.get('idCategorie')) !== -1;
});

AngularJS custom filter function

Inside my controller, I would like to filter an array of objects. Each of these objects is a map which can contain strings as well as lists
I tried using $filter('filter')(array, function) format but I do not know how to access the individual elements of the array inside my function. Here is a snippet to show what I want.
$filter('filter')(array, function() {
return criteriaMatch(item, criteria);
});
And then in the criteriaMatch(), I will check if each of the individual property matches
var criteriaMatch = function(item, criteria) {
// go thro each individual property in the item and criteria
// and check if they are equal
}
I have to do all these in the controller and compile a list of lists and set them in the scope. So I do need to access the $filter('filter') this way only. All the examples I found in the net so far have static criteria searches inside the function, they don't pass an criteria object and test against each item in the array.
You can use it like this:
http://plnkr.co/edit/vtNjEgmpItqxX5fdwtPi?p=preview
Like you found, filter accepts predicate function which accepts item
by item from the array.
So, you just have to create an predicate function based on the given criteria.
In this example, criteriaMatch is a function which returns a predicate
function which matches the given criteria.
template:
<div ng-repeat="item in items | filter:criteriaMatch(criteria)">
{{ item }}
</div>
scope:
$scope.criteriaMatch = function( criteria ) {
return function( item ) {
return item.name === criteria.name;
};
};
Here's an example of how you'd use filter within your AngularJS JavaScript (rather than in an HTML element).
In this example, we have an array of Country records, each containing a name and a 3-character ISO code.
We want to write a function which will search through this list for a record which matches a specific 3-character code.
Here's how we'd do it without using filter:
$scope.FindCountryByCode = function (CountryCode) {
// Search through an array of Country records for one containing a particular 3-character country-code.
// Returns either a record, or NULL, if the country couldn't be found.
for (var i = 0; i < $scope.CountryList.length; i++) {
if ($scope.CountryList[i].IsoAlpha3 == CountryCode) {
return $scope.CountryList[i];
};
};
return null;
};
Yup, nothing wrong with that.
But here's how the same function would look, using filter:
$scope.FindCountryByCode = function (CountryCode) {
// Search through an array of Country records for one containing a particular 3-character country-code.
// Returns either a record, or NULL, if the country couldn't be found.
var matches = $scope.CountryList.filter(function (el) { return el.IsoAlpha3 == CountryCode; })
// If 'filter' didn't find any matching records, its result will be an array of 0 records.
if (matches.length == 0)
return null;
// Otherwise, it should've found just one matching record
return matches[0];
};
Much neater.
Remember that filter returns an array as a result (a list of matching records), so in this example, we'll either want to return 1 record, or NULL.
Hope this helps.
Additionally, if you want to use the filter in your controller the same way you do it here:
<div ng-repeat="item in items | filter:criteriaMatch(criteria)">
{{ item }}
</div>
You could do something like:
var filteredItems = $scope.$eval('items | filter:filter:criteriaMatch(criteria)');

Resources