【问题标题】:Look up values in an array using looping forEach Google Apps Script Javascript使用循环 forEach Google Apps Script Javascript 在数组中查找值
【发布时间】:2020-10-26 18:53:01
【问题描述】:

我有一个如下所示的对象 {key: id numbers}

var obj = {
  "c4ecb": {id: [3]},
  "a4269": {id: [34,36]},
  "d76fa": {id: [54,55,60,61]},
  "58cb5": {id: [67]}
}

如何在下面的数组中循环 id 以上的每个,并返回 label

var response = 
    {
      "success": true,
      "data": [
        {
          "key": "c4ecb",
          "name": "fruits",
          "options": [
            {
              "label": "strawberry",
              "id": 3
            },
            {
              "label": "apple",
              "id": 4
            },
            {
              "label": "pineapple",
              "id": 5
            },
            {
              "label": "Other",
              "id": 31
            }
          ],
        }
      ]
    },
    {
      "success": true,
      "data": [
        {
          "key": "a4269",
          "name": "vegetables",
          "options": [
            {
              "label": "lettuce",
              "id": 34
            },
            {
              "label": "cucumber",
              "id": 35
            },
            {
              "label": "radish",
              "id": 36
            }
          ],
        }
      ]
    },
    {
      "success": true,
      "data": [
        {
          "key": "d76fa",
          "name": "pasta",
          "options": [
            {
              "label": "spaghetti",
              "id": 54
            },
            {
              "label": "rigatoni",
              "id": 55
            },
            {
              "label": "linguine",
              "id": 56
            },
            {
              "label": "lasagna",
              "id": 60
            },
            {
              "label": "fettuccine",
              "id": 61
            }
          ],
        }
      ]
    }

最后,我要做的是查找键并返回一串 id 值。 例如输入c4ecb,输出strawberry。输入a4269,输出lettuce, radish。输入d76fa,输出“spaghetti, rigatoni, lasagna, fettuccine

我认为将多个标签输出连接到一个字符串中,我可以使用类似的东西

array.data.vegetables.map(vegetables => vegetables.value).join(', ')].toString();

所以最后我想拥有类似的东西

var fruits = [some code that outputs "strawberry"];
var vegetables = [some code that outputs "lettuce, radish"];
var pasta = [some code that outputs "spaghetti, rigatoni, lasagna, fettuccine"];

到目前为止我已经尝试过: 仅当需要调用一个 id 时,以下循环才会返回 id:例如仅在{id: 3}的情况下,但在{id: 34,36}之类的情况下返回null(因为它在id中寻找'34,36',它不存在-我需要单独查找每一个。

response.data.forEach(({key, options}) => {
  if (obj[key]) {
    options.forEach(({id, label}) => {
      if (id == obj[key].id) obj[key].label = label;
    });
  }
});
console.log(obj)

【问题讨论】:

  • 您的 obj 不是有效的 JSON。我认为这是问题的一部分,但也许我误解了谷歌应用程序脚本。我本来想说你应该.split(",") ID 并为你找到的每一个做一个查找。
  • "a4269": {id: 34,36},//if id is a string then "id":"34,36" if it is an array then it should be "id":[34,36]
  • 我确实将 ids 更改为上面的数组。所以现在不是“id”:“34,36”,而是“id”:[34,36]。然后我需要返回一个像 [{"a4269": {label: [lettuce, radish]}}, {"d76fa": {label: [spaghetti, rigatoni, lasagna, fettucine]}}, ...] 这样的数组我需要 var vegetables = return 'lettuce, radish',然后 var Pasta = return 'spaghetti, rigatoni, lasagna, fetuccine' 等,我不知道如何处理

标签: javascript arrays google-apps-script


【解决方案1】:

过滤response 对象以关注与id 匹配的类别。 映射options 数组并选择出现在obj[id] 中的项目。 最后将过滤后的结果转成字符串。

请参阅下面的filteredLabelsAsString() 函数以进行实现。

var obj = {
  "c4ecb": {"id": [3]},
  "a4269": {"id": [34,36]},
  "d76fa": {"id": [54,55,60,61]},
  "58cb5": {"id": [67]}
}

var response = 
    [{
      "success": true,
      "data": [
        {
          "key": "c4ecb",
          "name": "fruits",
          "options": [
            {
              "label": "strawberry",
              "id": 3
            },
            {
              "label": "apple",
              "id": 4
            },
            {
              "label": "pineapple",
              "id": 5
            },
            {
              "label": "Other",
              "id": 31
            }
          ],
        }
      ]
    },
    {
      "success": true,
      "data": [
        {
          "key": "a4269",
          "name": "vegetables",
          "options": [
            {
              "label": "lettuce",
              "id": 34
            },
            {
              "label": "cucumber",
              "id": 35
            },
            {
              "label": "radish",
              "id": 36
            }
          ],
        }
      ]
    },
    {
      "success": true,
      "data": [
        {
          "key": "d76fa",
          "name": "pasta",
          "options": [
            {
              "label": "spaghetti",
              "id": 54
            },
            {
              "label": "rigatoni",
              "id": 55
            },
            {
              "label": "linguine",
              "id": 56
            },
            {
              "label": "lasagna",
              "id": 60
            },
            {
              "label": "fettuccine",
              "id": 61
            }
          ],
        }
      ]
    }];
 
 
 function filteredLabelsAsString(obj_key, obj, content=response) {
    // sanity check: obj must contain obj_key
    if (Object.keys(obj).includes(obj_key)) {
        return content.filter((item) => {
            // filter content using value of obj_key
            return item.data[0].key == obj_key;
        }).map((item) => {
            // item : { success: true, data: [] }
            // map over options array
            return item.data[0].options.map((opt) => {
                // option : {id, label}
                // return the label if the id is in the obj object's list
                if (obj[item.data[0].key].id.includes(opt.id))
                    return opt.label;
            }).filter((label) => {
                // filter out empty items
                return label !== undefined;
            });
        }).join(",");
    }
    // if obj does not contain obj_key return empty string
    return "";
}

console.log("fruits: " + filteredLabelsAsString("c4ecb", obj));

console.log("vegetables: " + filteredLabelsAsString("a4269", obj));

console.log("pasta: " + filteredLabelsAsString("d76fa", obj));

【讨论】:

  • 感谢上述过滤器。我可以运行代码 sn-p 并完美地查看结果日志。但是,当我将其粘贴到谷歌应用程序脚本中时,我收到一个错误:Cannot convert undefined or null to object (line 94),即if Object.keys(obj).includes(obj_key)) { 我不知道为什么它会显示为空。我不确定content = response 是否正确引用了上述响应变量,这就是它返回 null 的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
相关资源
最近更新 更多