artworkjpm
artworkjpm

Reputation: 1337

filter out array object to other array objects if the date matches

I have an array like this:

array = [
  { team: arsenal, date: "2019-12-21T12:30:00Z" },
  { team: mancity, date: "2019-12-21T12:30:00Z" },
  { team: chelsea, date: "2019-12-21T12:30:00Z" },
  { team: liverpool, date: "2019-12-22T12:30:00Z" },
  { team: spurs, date: "2019-12-22T12:30:00Z" }
];

I want to create other array objects with the dates that are the same, like this:

 array1 = [
    { team: arsenal, date: "2019-12-21T12:30:00Z" },
    { team: mancity, date: "2019-12-21T12:30:00Z" },
    { team: chelsea, date: "2019-12-21T12:30:00Z" },
  ];

  array2 = [
    { team: liverpool, date: "2019-12-22T12:30:00Z" },
    { team: spurs, date: "2019-12-22T12:30:00Z" }
  ];

I'm using vue.js but I guess this is just plain javascript solution. I've tried filter and for loops, I keep getting errors. Maybe lodash has a solution? whatever is most elegant.

My attempt:

arrayFilter() {
      for (var i = 0, dates = array.length; i < dates; i++) {
        var datearray = [];
        array.map((item, i) => {
          if (array[i] === item.date) {
            datearray.push(item.date);
          }
          console.log("datearray: ", datearray);
        });
      }
    }

Upvotes: 2

Views: 154

Answers (2)

Evgeny Korzun
Evgeny Korzun

Reputation: 91

Basically, you don't need any libraries. Just use reduce().

const array = [
  { team: 'arsenal', date: "2019-12-21T12:30:00Z" },
  { team: 'mancity', date: "2019-12-21T12:30:00Z" },
  { team: 'chelsea', date: "2019-12-21T12:30:00Z" },
  { team: 'liverpool', date: "2019-12-22T12:30:00Z" },
  { team: 'spurs', date: "2019-12-22T12:30:00Z" }
]

const grouppedObjectByDate = array.reduce((acc, item) => {
  (acc[item.date] || (acc[item.date] = [])).push(item)
  return acc
}, {})

console.log(grouppedObjectByDate)
// {
//   "2019-12-21T12:30:00Z": [
//     {"team": "arsenal", "date": "2019-12-21T12:30:00Z"},
//     {"team": "mancity", "date": "2019-12-21T12:30:00Z"},
//     {"team": "chelsea", "date": "2019-12-21T12:30:00Z"}
//   ],
//   "2019-12-22T12:30:00Z": [
//     {"team": "liverpool", "date": "2019-12-22T12:30:00Z"},
//     {"team": "spurs", "date": "2019-12-22T12:30:00Z"}
//   ]
// }

console.log(Object.values(grouppedObjectByDate))
// [
//   [
//     {"team": "arsenal", "date": "2019-12-21T12:30:00Z"},
//     {"team": "mancity", "date": "2019-12-21T12:30:00Z"},
//     {"team": "chelsea", "date": "2019-12-21T12:30:00Z"}
//   ],
//   [
//     {"team": "liverpool", "date": "2019-12-22T12:30:00Z"},
//     {"team": "spurs", "date": "2019-12-22T12:30:00Z"}
//   ]
// ]

Upvotes: 2

Ori Drori
Ori Drori

Reputation: 191976

Use _.groupBy() to split to arrays by date, and then use _.values() to get an array of arrays:

const array = [{"team":"arsenal","date":"2019-12-21T12:30:00Z"},{"team":"mancity","date":"2019-12-21T12:30:00Z"},{"team":"chelsea","date":"2019-12-21T12:30:00Z"},{"team":"liverpool","date":"2019-12-22T12:30:00Z"},{"team":"spurs","date":"2019-12-22T12:30:00Z"}]

const result = _.values(_.groupBy(array, 'date'))

console.log(result)

const [arr1, arr2] = result 

console.log(arr1, arr2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

Upvotes: 0

Related Questions