Home > Software design >  Javascript one-dimentional array of object "like" Filter
Javascript one-dimentional array of object "like" Filter

Time:07-31

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

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

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.

  • Related