1

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.

1
  • 1
    You should specify what exactly you need the results to be. Should the results contain all the objects that have a "Completed" status because of the "com" string? What about the case sensitivity? Commented Dec 8, 2021 at 18:58

5 Answers 5

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to work: returns an empty array.
@JasonFukura check again.
Thank you @Babis.amas! That looks like it's working perfectly!!!
0

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)

1 Comment

Thanks, but this doesn't work for searching all keys, which is the part that I am missing (plus excluding things that don't have both search terms)
0
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)
}

})

1 Comment

Thanks, but this doesn't return the array that I am looking to return (an array of 2 objects, as listed in the original post.)
0
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' }   
]

3 Comments

The output here doesn't match the output I'm looking for in the original post. Thx, tho.
check the updated code
thank you, this works, but the answer provided by @Babis.amas has the advantage of not needing to explicitly state what the keys in the object are. Thanks for responding!
0

You can use .filter() with Object.values(), RegExp() and .every() 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 => query.every(
        term => new RegExp(term, 'i').test( 
            Object.values(obj).join(" ")
        )            
    )
);
console.log( filtered );

EDIT

The code has been edited to use a RegExp() form and query combination that changes the logic on the query terms from OR to AND.

2 Comments

Thanks, but this doesn't return the array that I am looking to return (an array of 2 objects, as listed in the original post.)
So you're looking for both query terms, not at least one of them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.