【问题标题】:filter keys by names contains string按名称过滤键包含字符串
【发布时间】:2018-11-20 18:03:23
【问题描述】:

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

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

我已经用 .includes 尝试过这个,但它不起作用

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
        }];

【问题讨论】:

  • 请添加想要的结果...
  • 您是在比较item1item 还是item2item
  • item2 会发生什么?
  • 我将 item1 和 item2 与 item 进行比较以获取它们的颜色和组
  • 请将结果添加到问题中并添加为什么项目是一项而不是另一项。

标签: javascript arrays object filter include


【解决方案1】:

类似:

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;
})

【讨论】:

    【解决方案2】:

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

    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; }

    【讨论】:

    • 非常感谢您的帮助!但它是一个多维对象。我需要带有对象的简单数组。像这样 [{ shop:"shop1", "item": "my apple 1", "color": "red", "group": "my fruit", "score": 1},{ shop:"shop1 ", "item": "my apple 1", "color": "red", "group": "my fruit", "score": 1},etc]
    • 你要带什么东西? item1item2?
    • 我要带走所有物品
    • 第一个对象的数组应该如何查找?
    • 每一个物件都是一个独特的物品+独特的商店,分数是它们的总和
    【解决方案3】:

    另一种方法是使用函数 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; }

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      相关资源
      最近更新 更多