Get exact object which contains part of string from another array

Hi :),
I want to return an object which contains a part of a string from an array.
const array = ['UK', 'Germany', 'Sweden'];
const object = {text: "Stockton-on-Tees, UK"}

If the object.text contains part of string e.g ‘UK’ , I want to return that object {text: "Stockton-on-Tees, UK"}

Thank you for your time.

Be careful here.

Is Kirkuk, Iraq meant to be returned? Look closely…

Yes, exactly :slight_smile:

So…
object.text is the element to check.
Array.some() to quickly walk the array and return a boolean.
String.includes() is your test.

Give it a go yourself, see what you come up with.

I have tried this already:
array.some((el) => object.text.includes(el))
I’m getting boolean not the object.
So I need to find a different solution.

If the boolean is true, return the object. If it is false, return… whatever it is you want to return if it didn’t match.

With one object will be ok, but if I get 10 objects I want to return only those which contains the string.

You want to Array.filter() the array of objects, then, using the test you’ve already written to evaluate.

Ok, like this?

array.filter((el) => object.text.includes(el))

Then I’m getting value from an array, not object.

["UK"]

Not quite.

const array = ['UK', 'Germany', 'Sweden'];
const objects = [{text: "Stockton-on-Tees, UK"},{text: "Atlanta, GA, US"}]

Filter the objects, such that each object is evaluated by array.some((el) => object.text.includes(el))

1 Like

I’m not sure if you understand what I want to achieve. (Maybe my description is lack of sense)
I find the solution by changing the object to array and iterate it.

[object].filter((obj) =>
array.map((el) => el.includes(obj.text))
);

I don’t… quite think that works, but… you tell me.

const object = {text: "NotAPlace"}

Thank you for your advice and help!
I want to display an object which contains even part of a string which is common in the array, in this example ‘UK’, so the algorithm should return the {text: "Stockton-on-Tees, UK"}

asked and answered.

It seems that we are wanting to retrieve an object that matches the given country. We can use map to achieve that.

const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {

});

There can be several objects, so we use an objects array to store them all.

const objects = [
    {text: "Stockton-on-Tees, UK"},
    {text: "Twilight Hawker's Market, Perth, Australia"},
    {text: "Weihnachtsmarkt Am Kölner Dom, Cologne, Germany"}
];

In the map I want to find the first object that matches my requirements:

const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {
    return objects.find(function includesCountry(object) {
    
    });
});

and that requirement is where the object text matches the country.

const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {
    return objects.find(function includesCountry(object) {
        return object.text.includes(country);
    });
});

You end up with the locations array having three items, the first two are objects because they matched, and the last one is appropriately undefined.

[
    {text: "Stockton-on-Tees, UK"},
    {text: "Weihnachtsmarkt Am Kölner Dom, Cologne, Germany"},
    undefined
]
1 Like

I resolved it by myself. Please remove it.

Why not post how you solved it? It could possibly be of some help to someone in the future…

3 Likes

Of course.

[object].filter((obj) =>
array.map((el) => el.includes(obj.text))
);
3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.