2

I have one-dimentional array of object like this:

var data = [
{
  "empId": 63758,
  "empCode": "000003A",
  "empName": "Robert",
},
{
  "empId": 63759,
  "empCode": "000003B",
  "empName": "Paul John",
},
{
  "empId": 63760,
  "empCode": "000003C",
  "empName": "Chris John",
},
];

I want to filter the data just the way like when we use in sql query " like '%john%' ", and i wish it should be able to get non-case sensitive text as well. so it will output:

var data = [=
{
  "empId": 63759,
  "empCode": "000003B",
  "empName": "Paul John",
},
{
  "empId": 63760,
  "empCode": "000003C",
  "empName": "Chris John",
},
];

i already try this but the result is only accept full text parameter like "Chris John"

var filter = {
  "empName": "Chris John"
}
data = data.filter(function(item){
for(var key in filter){
    if(item[key] == undefined || item[key] != filter[key])
    return false
}
return true
});

Please help, thank you

smlos
  • 57
  • 1
  • 6
  • You have a one-dimensional array of objects. – PM 77-1 Jul 31 '22 at 02:48
  • You can use a [regex](https://stackoverflow.com/questions/48145432/javascript-includes-case-insensitive) or just [toLowerCase()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) and [includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) – freedomn-m Jul 31 '22 at 04:17

3 Answers3

3

You can simply achieve this by using String.indexOf() along with the Array.filter() method.

Live Demo :

var data = [
  {
    "empId": 63758,
    "empCode": "000003A",
    "empName": "Robert",
  },{
    "empId": 63759,
    "empCode": "000003B",
    "empName": "Paul John",
  },{
    "empId": 63760,
    "empCode": "000003C",
    "empName": "Chris John",
  }
];

const filter = {
  "empName": "john"
};

let res = data.filter(({ empName }) => empName.toLowerCase().indexOf(filter.empName) !== -1);

console.log(res);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
1

You can write a another util method to check the sub string values like below:

var data = [ { empId: 63758, empCode: "000003A", empName: "Robert", }, { empId: 63759, empCode: "000003B", empName: "Paul John", }, { empId: 63760, empCode: "000003C", empName: "Chris John", }, ];
var filter = {
  empName: "joh",
};

const checkLikeValue = (a, b) => {
  if(a === b) {
    return true
  } else if (typeof a === "string") {
    const substrings = a.split(" ");
    return substrings.some(str => b.toLowerCase().includes(str))
  }
  return false
}

data = data.filter(function (item) {
  for (var key in filter) {
    if (item[key] == undefined || !checkLikeValue(filter[key], item[key])) return false;
  }
  return true;
});

console.log(data);
Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
  • thank you for your respond, but it can't process parameter like "ohn", "joh", to get result contains that parameter – smlos Jul 31 '22 at 03:24
  • @smlos, swapped `filter[key]` and `item[key]` and added `toLowerCase` to ignore case, now works like that – Amila Senadheera Jul 31 '22 at 03:29
0

This can be achieved by using includes() as well as by indexOf() (as per the answer given by @Rohit Jindal).

Another approach would be to use includes.

var data = [
  {
    "empId": 63758,
    "empCode": "000003A",
    "empName": "Robert",
  },{
    "empId": 63759,
    "empCode": "000003B",
    "empName": "Paul John",
  },{
    "empId": 63760,
    "empCode": "000003C",
    "empName": "Chris John",
  }
];

const filter = {
  "empName": "john"
};

let res = data.filter(({ empName }) => empName.toLowerCase().includes(filter.empName));

console.log(res);

The only difference would be an additional check on the return value of indexOf() which is not needed when using includes(). Performance wise both clocks almost the same.

Jaisal Shah
  • 183
  • 1
  • 8