1

Hopefully I can articulate this.

The idea is that I need to combine several google docs into one document. I need to do this by location.

I have a sheet that includes the location and document id that I pulled into an object array (needBatches) and then grouped by the criteria (location):

const grouped = groupBy(needBatches, 'location');

function groupBy(objectArray, property) {
      return objectArray.reduce(function (acc, obj) {
        const key = obj[property];
        if (!acc[key]) {
          acc[key] = [];
        }
        acc[key].push(obj);
        return acc;
      }, {});
    }

Now I want to grab the document Ids stored in each array, so I try to do a forEach but get a type error: "Cannot find function forEach in object".

So I iterate the array like this:

 const arr = Object.keys(grouped).forEach(function(e) {        
        console.log(e, grouped[e])
    });

According to the console log it now looks like this:

Location1 [{documentLink1=<link>, documentId1=<id>, folder1=<folder>},{documentLink2=<link>, documentId2=<id>, folder2=<folder>}]

Location2 [{documentLink1=<link>, documentId1=<id>, folder1=<folder>},{documentLink2=<link>, documentId2=<id>, folder2=<folder>}]

It gives me everything on each row, but I really only want the document Id but I can't figure out how to call it in this format.

I've tried:

grouped[e].documentId
grouped[e]["documentId"]

I've just started working with object arrays so my knowledge is pretty limited. Can anyone assist me?

the grouped object looks like this: enter image description here

Here I'm bringing in only the rows that don't have a document:

    const needBatches = dataRange.filter(function(e){
        return e.documentLink !== ''
    })

which looks like this: enter image description here

Niya
  • 47
  • 1
  • 10
  • 1
    In your question, ``grouped[e].documentId`` and ``grouped[e]["documentId"]`` are used. But the key of ``documentId`` is not included in the console log you show. Are the values you want the values of ``documentId1`` and ``documentId2``? And in order to correctly understand your question, can you provide a sample value of ``grouped``? Of course, please remove your personal information. – Tanaike Jun 17 '19 at 23:30
  • @Tanaike, I've added a screenshot of `grouped` and I expanded one of the locations. Do you also want to see the data? It's basically a list of people with their location and document Id. And, yes, I only want the values in the documentId column but for each location. The idea is to pull them into an array so I can reference them when I go to merge the documents. – Niya Jun 18 '19 at 14:02
  • Have you tried with the map() function? You may take a look at this: https://stackoverflow.com/questions/14810506/map-function-for-objects-instead-of-arrays – Jescanellas Jun 18 '19 at 15:46
  • @Jescanellas Yes, I can pull the ids with map, but I'm struggling with pulling the ids by location. I'm probably overthinking it. I'll take a look at the link you provided and see if it helps me. Thanks! – Niya Jun 18 '19 at 16:24
  • @Niya Thank you for replying and adding the information. Unfortunately, from your additional information, I couldn't understand about the structure of your object. This is due to my poor skill. I deeply apologize for this. – Tanaike Jun 18 '19 at 22:27
  • Hi again, can you post your object "needBatches" declaration? Remember to hide all the personal information :) – Jescanellas Jun 20 '19 at 11:43
  • @Jescanellas I've added it above. Is this what you need? – Niya Jun 20 '19 at 15:15
  • Actually maybe I am overthinking this. All I need is to merge each row into the applicable document. If I've mapped the documentId into needBatches, shouldn't I be able to for each row, open the document id? – Niya Jun 20 '19 at 15:23
  • Hi again, sorry for the late reply. What about a for loop that checks the location of each object checking if needbatches[i].location = "Chicago'? Then you can push the needbatches[i].documentLink into another array. I think grouping all the objects is complicating it too much. – Jescanellas Jun 26 '19 at 10:32
  • Sorry for the late update! You're right, I was over complicating it. I scrapped the code and instead generated a document for each location. I then mapped the document id back to my original array which gave me the ability to reference it when merging all of the documents by location. It still needs some edits but the basic functionality is working :-) – Niya Jul 02 '19 at 21:55

1 Answers1

0

If the array and objects are like I understood, this is how I would iterate :

var Location1 = [{
  documentLink1: "Document 1 link1",
  documentId1: "Document 1 id1",
  folder1: "document 1 folder"
}, {
  documentLink2: "Document 1 link2",
  documentId2: "Document 1 id2",
  folder2: "document 1 folder2"
}]

var Location2 = [{
  documentLink1: "Document 2 link1",
  documentId1: "Document 2 id1",
  folder1: "document 2 folder"
}, {
  documentLink2: "Document 2 link2",
  documentId2: "Document 2 id2",
  folder2: "document 2 folder2"
}]

var arrayLocation = [Location1, Location2];

arrayLocation.forEach((location, idx) => {
  console.log(`Location number: ${idx}`)
  console.log(`${location[0].documentLink1}`);
  console.log(`${location[0].documentId1}`);
  console.log(`${location[0].folder1}`);
  console.log(`${location[1].documentLink2}`);
  console.log(`${location[1].documentId2}`);
  console.log(`${location[1].folder2}`);

})
Mikel
  • 411
  • 4
  • 8