0

Say I have an object that looks like this:

{
  "data": {
    "postsConnection": {
      "groupBy": {
        "author": [
          {
            "key": "xyz",
            "connection": {
              "aggregate": {
                "count": 5
              }
            }
          },
          {
            "key": "abc",
            "connection": {
              "aggregate": {
                "count": 3
              }
            }
          }
        ]
      }
    }
  }
}

How would one access the value of count corresponding to the author element that has, say, xyz as its key? I know for this particular example I could just do this:

const n = data.postsConnection.groupBy.author[0].connection.aggregate.count

But that would mean knowing in advance which element in the array holds the desired value for key, which isn't the case in my context.

TheLearner
  • 2,813
  • 5
  • 46
  • 94

5 Answers5

2

If the author can appear multiple times, you can .filter() the array stored at author and then .map() the results to the count:

const data = {data:{postsConnection:{groupBy:{author:[{key:"xyz",connection:{aggregate:{count:5}}},{key:"abc",connection:{aggregate:{count:3}}}]}}}};

const author = "xyz";
const res = data.data.postsConnection.groupBy.author.filter(({key}) => key === author).map(obj => obj.connection.aggregate.count);

console.log(res);

// If you want to get the total of all counts for the given author, you can use reduce on the result to sum:
const totalCountsForAuthor = res.reduce((acc, n) => acc+n, 0);
console.log(totalCountsForAuthor);

If the author can only appear once, you can use .find() instead of .filter() like so:

const data = {data:{postsConnection:{groupBy:{author:[{key:"xyz",connection:{aggregate:{count:5}}},{key:"abc",connection:{aggregate:{count:3}}}]}}}};

const author = "xyz";
const res = data.data.postsConnection.groupBy.author.find(({key}) => key === author).connection.aggregate.count

console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

You can use Array#find to get the first instance inside an array that meets a certain condition (in your case, the first instance whose key value is equal to the key value you want).

var obj = {"data":{"postsConnection":{"groupBy":{"author":[{"key":"xyz","connection":{"aggregate":{"count":5}}},{"key":"abc","connection":{"aggregate":{"count":3}}}]}}}};

function getAuthorByKey(key) {
  return obj.data.postsConnection.groupBy.author.find(author => author.key === key);
}

console.log(getAuthorByKey("xyz").connection.aggregate.count);
console.log(getAuthorByKey("abc").connection.aggregate.count);
nick zoum
  • 7,216
  • 7
  • 36
  • 80
1

If the author array always exists:

const data = {
      postsConnection: {
          groupBy: {
              author: [{
                  key: "xyz",
                  connection: {
                      aggregate: {
                          count: 5
                      }
                  }
              }, {
                  key: "abc",
                  connection: {
                      aggregate: {
                          count: 3
                      }
                  }
              }]
          }
      }
};

function getCount(keyVal) {
    const element = data.postsConnection.groupBy.author.find(item => item.key === keyVal)
    return element.connection.aggregate.count || "";
}

console.log(getCount('xyz'))
Daniyal Lukmanov
  • 1,149
  • 10
  • 21
1

var data = { "data": {
    "postsConnection": {
      "groupBy": {
        "author": [
          {
            "key": "xyz",
            "connection": {
              "aggregate": {
                "count": 5
              }
            }
          },
          {
            "key": "abc",
            "connection": {
              "aggregate": {
                "count": 3
              }
            }
          }
        ]
      }
    }
  }
  };
  
  data.data.postsConnection.groupBy.author.forEach((autor) => {
  if(autor.key === "xyz")
    console.log(autor.connection.aggregate);
  });
Ivan Karaivanov
  • 343
  • 1
  • 9
1

You can make use of array find, to find the author by "key".

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

const author = data.postsConnection.groupBy.author.find((author) => author.key === "xyz")

CodePen: https://codepen.io/gmaslic/pen/wvwbLoL

Then you can access all the properties from found "author"

gmaslic
  • 54
  • 5