AngularJS: How do I check if date exist in array and return id?

check if value already exists in array javascript
javascript check if value exists in array of objects
check if value already exists in array jquery
angularjs check if value exists in array of objects
check if value exists in array angular 6
check if value exists in array java
angularjs check if array contains object
angular 4 check if array contains value

I'm trying to check if the current date exists in an array and return id which exists on the same index where date exists.

The below code is not working to check the element, I have already tried includes()

  const results = []; 
angular.forEach(getEventdate, function(value)
{
results.push({id:value.id,event_date:value.event_date}); });

    if(results.some(result => result.event_date === current_date)) 
{ 
console.log('date exists!'); 
}

Sample array:

0: {id: 4, event_date: "2019-01-11"}
1: {id: 6, event_date: "2019-01-11"}
2: {id: 7, event_date: "2019-01-11"}
3: {id: 8, event_date: "2017-06-13"}
4: {id: 9, event_date: "2017-06-14"}
5: {id: 10, event_date: "2017-06-21"}
6: {id: 11, event_date: "2017-06-22"}
7: {id: 12, event_date: "2017-06-23"}
8: {id: 13, event_date: "2017-06-26"}
9: {id: 14, event_date: "2017-06-27"}

I need id if a current date exists in the array

With ES2015+ you can use find to get the id when it's found and undefined otherwise.

const { id } = values.find(item => item.event_date === currentDate) || {};

How to Check If a Value Exists in an Array in JavaScript, You can use the indexOf() method to check whether a given value or element exists in an array or not. The indexOf() method returns the index of the element  How can I check if an element with a specific ID already exists in my DOM inside an Angular directive? By using angular.element() an element is created if one does not exist, for example angular.element('someID') will return an element whether one already exist or not.

If you want get the very first occurrence of the date then try using find, or if you want all the occurrences matching the date then try using filter.

e.g. using find

const {id} = results.find(val=>{
   return val.event_date === someDateString;
})

Filter will return array of all the occurrences matching that value.

const dates=results.filter(val=>{
    return val.event_date === someDateString;
})

Check if value exists in Array - jQuery and JavaScript, If you are familiar with PHP, where you can use the in_array() function to search value in the Array and its returns the Boolean value ( TRUE or  AngularJs Check if a key exists in an object Example You can simply use the (“key” in obj1) to check a key in an object it will return false if the key does not exist in object else it will return true.

You can use $filter.

var d = new Date();
var filteredDate = $filter('date')(d, 'yyyy-M-d');
var foundId = (array.filter(function(item) {
    return item.event_date === filteredDate;
})[0] || {}).id;

This will return undefined if the date is not found.

You will need to inject $filter into your controller.

AngularJS: API: $resource, Each key value in the parameter object is first bound to url template if present and then The transform function takes the http request body and headers and returns its To prevent this behavior, set transformRequest to an empty array: var cards = CreditCard.query(); // GET: /users/123/cards // server returns: [{id: 456,​  Execute script when the Array contains the particular value. etc. If you are familiar with PHP, where you can use the in_array() function to search value in the Array and its returns the Boolean value ( TRUE or FALSE ). There are inbuilt methods in jQuery and JavaScript that return the index position of the value which you can use for the search.

angular.forEach, fromJson · angular.identity · angular.injector · angular. Invokes the iterator function once for each item in obj collection, which can be either an object or an array. key, obj) , where value is the value of an object property or an array element, key for obj will not throw a TypeError, but rather just return the value provided. javascript,jquery,date I have two long dates value. For Ex: 1433097000000 1434479400000 Here i have to find the days between these two by jquery For Ex: 15/06/2015 - 22/06/2015 If it is more than 5 days, I want to get from is 15/06/2016, to is 19/06/2015.

filter, fromJson · angular.identity · angular.injector · angular. Note: If the array contains objects that reference themselves, filtering is not For example {name:"​M", phone:"1"} predicate will return an array of items which Objects are not compared against primitives, unless they have a custom toString method (e.g. Date objects). an element id is never undefined or null- most html elements have an id property, though until it has been assigned, in the html or a script, its value is '', the empty string.

JavaScript Array includes() Method, JavaScript Reference HTML DOM Reference jQuery Reference AngularJS Reference W3.JS Reference This method returns true if the array contains the element, and false if not. Note: The Default 0. At which position in the array to start the search Check if an array includes "Banana", starting the search at position 3:. What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

Comments
  • I replaced 'data' with my source array but its not working
  • @JKLM I apologize, I misspelled data. It's supposed to be date. It's a string telling the $filter function which filter to use. I edited my answer.