简体   繁体   中英

Filter array of objects by array strings and match with substrings of the object

Actual Code
//return arrayOfObjects.filter((object) => searchTags.every((tag) => Object.values(object).includes(tag)))

array of objects =

let searchTags= ['damiencbib','ADLs'];
    {
        "key": "ACCOR02a",
        "type": "techreport",
        "AUTHOR": "ACCORD",
        "INSTITUTION": "ACCORD",
        "KEYWORDS": "damiencbib adl",
        "MONTH": "June",
        "TITLE": "'Etat de l'art sur les Langages de Description d'Architecture (ADLs)",
        "URL": "http://projects.infres.enst.fr/accord/",
        "YEAR": "2002",
        "BDSK-URL-1": "http://projects.infres.enst.fr/accord/"
    },
    {
        "key": "ACM94b",
        "type": "article",
        "AUTHOR": "ACM",
        "INSTITUTION": "ACM",
        "JOURNAL": "Communications of the ACM",
        "KEYWORDS": "scglib",
        "MONTH": "May",
        "NUMBER": "5",
        "TITLE": "Reverse Engineering",
        "VOLUME": "37",
        "YEAR": "1994"
    }

Expected result is only the first object in the array because it contains both 'damiencbib','ADLs' inside. Something is not working as expected for me, I was thinking to use Regex. Thank you in advance.

You need to check some value as well.

 const searchTags = ['damiencbib', 'ADLs'], data = [{ key: "ACCOR02a", type: "techreport", AUTHOR: "ACCORD", INSTITUTION: "ACCORD", KEYWORDS: "damiencbib adl", MONTH: "June", TITLE: "'Etat de l'art sur les Langages de Description d'Architecture (ADLs)", URL: "http://projects.infres.enst.fr/accord/", YEAR: "2002", "BDSK-URL-1": "http://projects.infres.enst.fr/accord/" }, { key: "ACM94b", type: "article", AUTHOR: "ACM", INSTITUTION: "ACM", JOURNAL: "Communications of the ACM", KEYWORDS: "scglib", MONTH: "May", NUMBER: "5", TITLE: "Reverse Engineering", VOLUME: "37", YEAR: "1994" }], result = data.filter(object => searchTags.every(tag => Object .values(object) .some(s => s.includes(tag)) )); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM