0

My data seems to be like this:

const myObj = {
  "incidents": [{
    "id": 4,
    "fullName": "edsadas",
    "address": "Bagbaguin, Pandi, Bulacan",
  }, {
    "id": 5,
    "fullName": "reasdsa",
    "address": "Dalig, Balagtas, Bulacan",
  }, {
    "id": 6,
    "fullName": "dsa",
    "address": "Dalig, Balagtas, Bulacan",
  }],
}

My question, how can i return the similar values with count, like this:

{
  "Dalig, Balagtas, Bulacan": 2,
  "Bagbaguin, Pandi, Bulacan": 1
}
3
  • the address key Commented Mar 6, 2018 at 17:27
  • There is some difference between same and similar... Commented Mar 6, 2018 at 17:27
  • ill edit the question, sorry man Commented Mar 6, 2018 at 17:28

2 Answers 2

5

You can use the function reduce:

var obj = {  "incidents": [{      "id": 4,      "fullName": "edsadas",      "address": "Bagbaguin, Pandi, Bulacan",    },    {      "id": 5,      "fullName": "reasdsa",      "address": "Dalig, Balagtas, Bulacan",    },    {      "id": 6,      "fullName": "dsa",      "address": "Dalig, Balagtas, Bulacan",    }  ]};
var result = obj.incidents.reduce((a, c) => {
            a[c.address] = (a[c.address] || 0) + 1
            return a;
         }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using reduce and comma operator:

var obj = {  "incidents": [{      "id": 4,      "fullName": "edsadas",      "address": "Bagbaguin, Pandi, Bulacan",    },    {      "id": 5,      "fullName": "reasdsa",      "address": "Dalig, Balagtas, Bulacan",    },    {      "id": 6,      "fullName": "dsa",      "address": "Dalig, Balagtas, Bulacan",    }  ]},         
    result = obj.incidents
                .reduce((a, c) => (a[c.address] = (a[c.address] || 0) + 1, a), {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

Ele, your solution is perfect. It could be even more compact using comma operator. Fiddle
Ugh, please don't use the comma operator for this. It does nothing to add clarity to the code.
@RannieOllit Thank you is also good, but accepting the answer that solved your problem is a better way :)
1

You can iterate over them and increment the value every time. If not found just assign 1

const data = [
  {
     "id": 4,
     "fullName": "edsadas",
     "address": "Bagbaguin, Pandi, Bulacan",
  },
  {
     "id": 5,
     "fullName": "reasdsa",
     "address": "Dalig, Balagtas, Bulacan",
  },
  {
     "id": 6,
     "fullName": "dsa",
     "address": "Dalig, Balagtas, Bulacan",
}];

const count = data.reduce((acc, item) => (acc[item.address] = acc[item.address] ? acc[item.address] + 1 : 1, acc), {});

console.log(count);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.