3

Received JSON response

[{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]

Expected output

[
  {
    "name": "A1",
    "date": "2019-03-13",
    "data": [
      {
        "name": "A1",
        "date": "2019-03-13",
        "comment": "xyz"
      },
      {
        "name": "A1",
        "date": "2019-03-13",
        "comment": "abc"
      }
    ],
    {
      "name": "A1",
      "date": "2019-03-14",
      "data": [
        {
          "name": "A1",
          "date": "2019-03-14",
          "comment": "mno"
        }
      ]
    },
    {
      "name": "B1",
      "date": "2019-03-13",
      "data": [
        {
          "name": "B1",
          "date": "2019-03-13",
          "comment": "pqr"
        }
      ]
    }
  ]

I want to create new javascript array which contains unique name & date and its respective data.

Can someone help me here?

1
  • 3
    Why the duplication of data? Commented Mar 13, 2019 at 13:43

4 Answers 4

3

You can use array#reduce to group date based on the name & date in an object accumulator, then extract all values using Object.values().

let data = [{ "name": "A1", "date": "2019-03-13", "comment": "xyz" }, { "name": "A1", "date": "2019-03-13", "comment": "abc" }, { "name": "B1", "date": "2019-03-13", "comment": "pqr" }, { "name": "A1", "date": "2019-03-14", "comment": "mno" } ],
    result = Object.values(data.reduce((r,{name,date, comment}) => {
      r[name + "_" + date] = r[name + "_" + date] || {name, date, data: []};
      r[name + "_" + date].data.push({name,date,comment});
      return r;
    },{}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

I took more time to post this question compare to your so quick response. Thanks a ton!!!
3

You could take an array for the wanted grouping and look for the object.

var data = [{ name: "A1", date: "2019-03-13", comment: "xyz" }, { name: "A1", date: "2019-03-13", comment: "abc" }, { name: "B1", date: "2019-03-13", comment: "pqr" }, { name: "A1", date: "2019-03-14", comment: "mno" }],
    groupBy = ['name', 'date'],
    grouped = data.reduce((r, o) => {
        var temp = r.find(p => groupBy.every(k => o[k] === p[k]));
        if (!temp) {
            r.push(temp = Object.assign(...groupBy.map(k => ({ [k]: o[k] })), { data: [] }));
        }
        temp.data.push(o);
        return r;
    }, []);

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

Comments

2

You can use reduce to collect the values:

var input = [{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]

var res = input.reduce((acc, {name, date, comment}) => {
    var found = acc.find(el => el.name === name && el.date === date);
    return found 
        ? found.data.push({name, date, comment}) && acc 
        : [...acc, {name, date, data: [{name, date, comment}]}];
}, []);

console.log(JSON.stringify(res));

Comments

0

an Easy way to do that :

const data = [{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
   {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "fffffffff"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
   {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "hhhhhhhhhhh"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]


const res = data.reduce((all, acc) => {

  const found = all.find(o => o.name === acc.name && o.date === acc.date)

  if (found === undefined) {
    all.push(acc)
  } else {
    found.data = (found.data || []).concat(acc)
  }

  return all;

}, [])

console.log(res)

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.