Skip to content Skip to sidebar Skip to footer

Look Up Values In An Array Using Looping ForEach Google Apps Script Javascript

I have an object that looks like the following {key: id numbers} var obj = { 'c4ecb': {id: [3]}, 'a4269': {id: [34,36]}, 'd76fa': {id: [54,55,60,61]}, '58cb5': {id: [67]} }

Solution 1:

Filter the response object to focus on the category that matches the id. Map over the options array and select the items which appear in obj[id]. Finally convert the filtered results to a string.

See filteredLabelsAsString() function below for implementation.

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

Post a Comment for "Look Up Values In An Array Using Looping ForEach Google Apps Script Javascript"