Javascript map problem

Discussion in 'JavaScript' started by sarahk, Mar 13, 2022.

  1. #1
    I have a rest call that returns me data in the following fomat. To use it I need it to look like my "desired" variable but I'm not even getting close. What should I be doing?

    const result = [
      {
        document: {
          name: "projects/gvLygwLmSqyPNlYI6mOV",
          fields: {
            phone: { stringValue: "888998" },
            name: { stringValue: "Test 1" },
            claimed: { booleanValue: false }
          },
          createTime: "2022-03-11T10:34:45.795450Z",
          updateTime: "2022-03-12T07:58:02.209421Z"
        },
        readTime: "2022-03-14T04:32:45.429598Z"
      },
      {
        document: {
          name: "projects/ldkfgalfkjsdlfkjs",
          fields: {
            phone: { stringValue: "12345" },
            name: { stringValue: "Test 2" },
            claimed: { booleanValue: false }
          },
          createTime: "2022-03-11T10:34:45.795450Z",
          updateTime: "2022-03-12T07:58:02.209421Z"
        },
        readTime: "2022-03-14T04:32:45.429598Z"
      }
    ];
    ("projects/gvLygwLmSqyPNlYI6mOV");
    const desired = {
      companies: [
        {
          path: "projects/gvLygwLmSqyPNlYI6mOV",
          name: "Test 1",
          phone: "888998",
          claimed: false
        },
        {
          path: "projects/ldkfgalfkjsdlfkjs",
          name: "Test 2",
          phone: "12345",
          claimed: false
        }
      ]
    };
    let output = new Map();
    output.companies = result.map((company) => {
      let fields = new Map();
      fields.name = company.document.name;
      const fieldsMap = company.document.fields;
      for (const [fieldName, fieldDesc] of Object.entries(fieldsMap)) {
        const fieldValue = fieldDesc[Object.keys(fieldDesc)[0]];
        fields.set(fieldName, fieldValue);
        console.log("fieldDesc: ",fieldDesc);
      }
      return fields;
    });
    console.log(output);
    console.log("output: ", [...output.companies.entries()]);
    console.log(JSON.stringify(output));
    
    Code (javascript):
    and here's what I get logged from output.companies.entries()

    upload_2022-3-14_17-54-52.png

    and from the stringify - the values added by .set() are excluded
    "{'companies':[{'name':'projects/gvLygwLmSqyPNlYI6mOV'},{'name':'projects/ldkfgalfkjsdlfkjs'}]}"
    Code (markup):
    What is the right way to rework the array to get my "desired" structure?
     
    Last edited: Mar 13, 2022
    sarahk, Mar 13, 2022 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #2
    ok, so I have this working
    const result = [
      {
        document: {
          name: "projects/gvLygwLmSqyPNlYI6mOV",
          fields: {
            name: { stringValue: "Test 1" },
            phone: { stringValue: "88878" },
            claimed: { booleanValue: false }
          },
          createTime: "2022-03-11T10:34:45.795450Z",
          updateTime: "2022-03-12T07:58:02.209421Z"
        },
        readTime: "2022-03-14T04:32:45.429598Z"
      },
      {
        document: {
          name: "projects/ldkfgalfkjsdlfkjs",
          fields: {
            name: { stringValue: "Test 2" },
            phone: { stringValue: "12345" },
            claimed: { booleanValue: false }
          },
          createTime: "2022-03-11T10:34:45.795450Z",
          updateTime: "2022-03-12T07:58:02.209421Z"
        },
        readTime: "2022-03-14T04:32:45.429598Z"
      }
    ];
    
    const desired = {
      companies: [
        {
          path: "projects/gvLygwLmSqyPNlYI6mOV",
          name: "Test 1",
          phone: "888998",
          claimed: false
        },
        {
          path: "projects/ldkfgalfkjsdlfkjs",
          name: "Test 2",
          phone: "12345",
          claimed: false
        }
      ]
    };
    
    const reformatResult = function (result) {
      return result.map(function (company) {
        const newMap = { path: company.document.name };
        const fields = reformatFields(company.document.fields);
        var merged = { ...newMap, ...fields };
        return merged;
      });
    };
    
    const reformatFields = function (fields) {
      const output = new Map();
      const arr = Object.entries(fields);
    
      arr.forEach((entry) => {
        const label = entry[Object.keys(entry)[0]];
        const valMap = entry[1];
    
        if ("stringValue" in valMap) val = valMap.stringValue;
        else if ("booleanValue" in valMap) val = valMap.booleanValue;
        else val = "???";
    
        output[label] = val;
      });
    
      return output;
    };
    
    console.log("reformatted", reformatResult(result));
    Code (JavaScript):
    but I'd like a better way of dealing with this bit where I don't care what the property is called, there will be only one and I just want the value.
        if ("stringValue" in valMap) val = valMap.stringValue;
        else if ("booleanValue" in valMap) val = valMap.booleanValue;
        else val = "???";
    Code (JavaScript):
     
    sarahk, Mar 14, 2022 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I've got the second function down to this now, so much more complicated than working with PHP

    const reformatFields = function (fields) {
      const output = new Map();
      const arr = Object.entries(fields);
    
      arr.forEach((entry) => {
        const label = entry[Object.keys(entry)[0]];
        const valMap = entry[1];
        output[label] = Object.entries(valMap)[0][1];
      });
    
      return output;
    };
    Code (JavaScript):
     
    sarahk, Mar 14, 2022 IP