Home > database >  How to filter an array of javascript objects based on an array of strings
How to filter an array of javascript objects based on an array of strings

Time:12-09

Given the following object, I would like to search all of the keys for multiple strings. I've been trying and searching and have come up empty on this one. Can anyone provide me some help here?

Searching this:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];

For this array of strings:

const query = ['chris', 'com'];

Would return an array of results like this:

[
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
]

Please let me know if I'm not being clear on this. Thanks!

// UPDATE

Note that the key thing I'm looking for is that each object must be able to be searched by ANY key (not just creator), and have ALL search strings in the object, not just one or the other.

CodePudding user response:

const array = [{
    name: "Blue Iron Chow Chow",
    status: "Complete",
    creator: "John"
  },
  {
    name: "Purple Steel Husky",
    status: "Error",
    creator: "Chris"
  },
  {
    name: "Purple Composite Husky",
    status: "Ready",
    creator: "Chris"
  },
  {
    name: "Aqua Zinc Spaniel",
    status: "Complete",
    creator: "Chris"
  },
  {
    name: "Fuschia Silver Corgi",
    status: "Complete",
    creator: "John"
  },
];

const query = ['chris', 'com'];

const filtered = array.filter(obj => {
  return query.every(val =>
  Object.values(obj).join(" ").toLowerCase().includes(val.toLowerCase()))
})

console.log(filtered)

CodePudding user response:

You could try this:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];

const query = ['chris', 'com'];

const result = array.filter(({creator}) => query.includes(creator.toLowerCase()))

console.log(result)

CodePudding user response:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];

let filtered = []


array.map((x)=>{
//x contains individual objects of array,
for (let key in x) {
//key in x will get all the keys inside the x , now all we have to check is does the values in x are included in query or not to find that we use the below code
  const contains = ['Chris', 'com'].includes(x[key])
   console.log(contains)
  if(contains) filtered.push(x)
}

})

CodePudding user response:

You can use .filter() with Object.values(), RegExp() and .some() methods, as in the demo below:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];
const query = ['chris', 'com'];
const filtered = array.filter(
    obj => Object.values(obj).some(
        val => new RegExp(query.join('|'),'i').test(val)
    )
);
console.log( filtered );

CodePudding user response:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];


const query = ['chris', 'com'];

var result = array.filter(({creator, name, status}) => {
    return query.every(item => {
            item = item.toLowerCase();
            return creator.toLowerCase().includes(item) || name.toLowerCase().includes(item) || status.toLowerCase().includes(item)
        })
});

output:-

[    
  { name: 'Purple Composite Husky', status: 'Ready', creator: 'Chris' },
  { name: 'Aqua Zinc Spaniel', status: 'Complete', creator: 'Chris' }   
]
  • Related