按名称过滤键包含字符串

时间:2018-06-11 13:11:10

标签: javascript arrays object filter include

我想在myArray键中按名称包含字符串“item”(所有“item1”,“item2”,“item3”等)进行过滤

const combined = myArray.map(e => Object.assign(e, MyArrayDefinition.find(k => k.item === e.item)));

我用.include尝试了这个但是它不起作用

const combined = myArray.map(e => Object.assign(e, MyArrayDefinition.find(k => k.includes('item') === e.item)));

我的阵列

const myArray = [{
            "shop": "shop1",
            "item1": "my apple 1",
            "item2": "my carrot 1",

        },  {
            "shop": "shop2",
            "item1": "my apple 0",
            "item2": "my carrot 1",

        }, {
            "shop": "shop2",
            "item1": "my apple 1",
            "item2": "my carrot 0",

        }, ];

        const MyArrayDefinition = [ {
            "item": "my apple 0",
            "color": "red",
            "group": "my fruit",
            "score": 0
        }, {
            "item": "my carrot 1",
            "color": "orange",
            "group": "my vegetable",
            "score": 1
        }, {
            "item": "my apple 1",
            "color": "red",
            "group": "my fruit",
            "score": 1
        }, {
            "item": "my carrot 0",
            "color": "orange",
            "group": "my vegetable",
            "score": 0
        }];

3 个答案:

答案 0 :(得分:0)

类似的东西:

const filtered = myArray.map((obj) => {
    const objKeys = Object.keys(obj);
    const result = objKeys.reduce((acc, k) => {
        if (k.match(/item/)) { // just keys withe "item"
            acc[k] = obj[k];
        }

        return acc;
    }, {});

    return result;
})

答案 1 :(得分:0)

如果需要所有找到的项目的数组,那么您可以过滤键并获取定义数组的项目。



var array = [{ shop: "shop1", item1: "my apple 1", item2: "my carrot 1" }, { shop: "shop2", item1: "my apple 0", item2: "my carrot 1" }, { shop: "shop2", item1: "my apple 0", item2: "my carrot 0" }],
    definition = [{ item: "my apple 0", color: "red", group: "my fruit", score: 0 }, { item: "my carrot 1", color: "orange", group: "my vegetable", score: 1 }, { item: "my apple 1", color: "red", group: "my fruit", score: 1 }, { item: "my carrot 0", color: "orange", group: "my vegetable", score: 0 }],
    result = array.map(o => ({
        shop: o.shop,
        items: Object
            .keys(o)
            .filter(k => k.startsWith('item'))
            .map(k => definition.find(({ item }) => item === o[k]))
    }));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

另一种方法是使用函数filter和函数includes来过滤那些带有键=== item的对象,并与数组MyArrayDefinition一起存在。

此示例具有以下对象,该值不存在于MyArrayDefinition

{  
    "shop": "shop33",
    "item1": "my apple 33",  
    "item2": "my carrot 33"
}



const myArray = [{  "shop": "shop1",  "item1": "my apple 1 my fruit",  "item2": "my carrot 1",}, {  "shop": "shop2",  "item1": "my apple 0",  "item2": "my carrot 1",}, {  "shop": "shop2",  "item1": "my apple 0",  "item2": "my carrot 0",}, {  "shop": "shop33",  "item1": "my apple 33",  "item2": "my carrot 33",}],
      MyArrayDefinition = [{  "item": "my apple 0",  "color": "red",  "group": "my fruit",  "score": 0}, {  "item": "my carrot 1",  "color": "orange",  "group": "my vegetable",  "score": 1}, {  "item": "my apple 1",  "color": "red",  "group": "my fruit",  "score": 1}, {  "item": "my carrot 0",  "color": "orange",  "group": "my vegetable",  "score": 0}],
      mappedItems = MyArrayDefinition.map(({item}) => item), // convert to an array with only values from item.
      result = myArray.filter(o => Object.keys(o).some(k => k.includes('item') && mappedItems.includes(o[k]))); // get the object with at least one key === item and which value exists within mappedItems.

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }