0

I have an array of this form:

[
  {
    username: '',
    id: '',
    moreInfo: {
      infoDate: ''
    }
  }
]

And I need to filter based on the infoDate, whether it's in between two specific dates.

I have a function that accepts the object, and the field to search range by and returns :

return resultDate >= fromDate && resultDate <= thruDate;

But how do I filter such array . I tried

userData.filter(userData => functionthatFiltersDate(userData.moreInfo, {from,thru}, 'infoDate')

The functionthatFiltersDate is a function that accepts an object as input and dates to check range :

functionthatFiltersDate = (
  result,
  { fromDate, thruDate },
  fieldName
) => {
  let resultDate = result[fieldName];
  if (!isDate(resultDate)) {
    resultDate = moment(result[fieldName]).toDate();
  }
  if  (!isDate(fromDate)) {
    fromDate = moment(fromDate).toDate();
  }
  if (!isDate(thruDate)) {
    thruDate = moment(thruDate).toDate();
  }
  return resultDate >= fromDate && resultDate <= thruDate;
};

How do I filter though for an array of objects, based on another object property that's inside? Any help is appreciated!

  • Your code works? You know that `filter` returns a new array? – Jonas Wilms Jul 19 '18 at 20:11
  • @JonasW. It works when I have a different array of objects , like : `[{name:'', date:''}, {name:'', date:''}` , not the date being inside another object –  Jul 19 '18 at 20:15
  • `userData.moreInfo,` must be`user.moreInfo` – Jonas Wilms Jul 19 '18 at 20:16
  • Ah sorry, I do write it correctly in the code, I was just trying to put a mock array here to shorten it. But the filtering doesn't work. I get empty array. –  Jul 19 '18 at 20:19
  • Maybe because `from` isnt `fromDate` ?! You made so many typos while "simplifying" your code that its impossible to tell what the root cause is in your original code – Jonas Wilms Jul 20 '18 at 08:21

1 Answers1

-1

call your function this way:

function fooBar(item ){
    // ....
    // return truthy condition
};

var result = arr.filter(fooBar);

-

Passing argument to the filter function:

const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];

/**
 * Array filters items based on search criteria (query)
 */
const filterItems = (query) => {
  return fruits.filter((el) =>
    el.toLowerCase().indexOf(query.toLowerCase()) > -1
  );
}

console.log(filterItems('ap')); // ['apple', 'grapes']
console.log(filterItems('an')); // ['banana', 'mango', 'orange']

Source: Mozilla Docs

-

Filtering array based on nested values:

var arr = [
 {foo: 'item1', nested: {nestedFoo: 'returnThis'} },
 {foo: 'item2', nested: {nestedFoo: 'notThis'} },
];

var result = arr.filter(item => item.nested.nestedFoo == 'returnThis');
// result variable now contains an array with one object
console.log(result) // array
console.log(result[0]) // object
console.log(result[0].foo) // 'item1'

Refer to this question for explanation on how to access nested values

-

Making it more clear:

var arr = [
  {
    username: '',
    id: '',
    moreInfo: {
      infoDate: ''
    }
  },
  {
    username: '',
    id: '',
    moreInfo: {
      infoDate: ''
    }
  },
 ...
];

const filterItems = (fromDate, thruDate, fieldName) => {
  return arr.filter((item) =>
    item.moreInfo.infoDate >= fromDate && item.moreInfo.infoDate < thruDate
  );
}
Enrico
  • 408
  • 6
  • 13
  • 1
    Why didn't you use OP's example for your answer? It would have been easier to follow with just a few changes from the original code. – David Knipe Jul 19 '18 at 22:42
  • As stated by OP in the comments under his questions, he mocked the original code in order to simplify the question and a specific answer may be too narrow. This might also help OP with a better understanding of the concept – Enrico Jul 19 '18 at 23:05
  • @Enrico Thank you, but you are filtering ... if the word contains those letters? What I can't do is if I have an array of objects, and then that array has an object within an object.. as stated in the example, how to filter based of one field value. –  Jul 20 '18 at 01:04
  • My function can even be ignored for simplification, I just don't know how to filter when the field I need to filter from is that deeply nested. –  Jul 20 '18 at 01:05
  • I don't think that some samples copied from MDN are able to solve a specific problem with the code. – Jonas Wilms Jul 20 '18 at 08:22
  • OP has problems using the filter function and accessing nested properties. The code I provided solves both. Also, I'm providing more than mdn examples. – Enrico Jul 20 '18 at 11:29