1

I have this array of dates.

const dateArray = ["7/12/2021","7/13/2021","7/14/2021","7/15/2021"]

and I am trying to see if any of these dates appear inside my other array of array of objects, those dates will match something inside the objects.

const data = [ 
      [ { id: 1, date: "7/13/2021" }, { id:2, date: "7/15/2021" } ],
      [ { id: 1, date: "7/14/2021" }, { id:2, date: "7/15/2021" } ],
]

so if date doesn't match I want to return 0 for that date. something like this

const result = [
     [0, 1, 0, 1],
     [0, 0, 1, 1]
]

I tried something like this.....

var getResult = (dateArray, data) => {
    const result = []
    let index = 0;
    for (let i = 0; i < dateArray.length; i++) {
        for (let j = 0; j < data.length; j++) {
            let count = 0
            if (dateArray[i] === data[j][index].date) {
                count++
                index++
                if (index === data[i].length) {
                    return
                }
            }
            result.push(count)
        }
    }
    console.log(result)
}

but it doesn't work of course.... Thank you for your help!

Thorai219
  • 53
  • 5
  • Is the result the expected output for the example ``data``? Can you explain more – Majed Badawi Jul 11 '21 at 19:21
  • so to get this clear first check if the date in the data array exists in the ```dateArray``` array then if it exists write 1 in the result array and if it doesn't exist write 0 in the result array? – seriously Jul 11 '21 at 19:22
  • yes the result is the expected output, and it's supposed to count the occurences of date from dateArray inside data, I am sorry I am not clear, but is this possible? – Thorai219 Jul 11 '21 at 19:24

2 Answers2

2

const 
  dateArray = ["7/12/2021","7/13/2021","7/14/2021","7/15/2021"],
  data = [ 
    [ { id: 1, date: "7/13/2021" }, { id:2, date: "7/15/2021" } ],
    [ { id: 1, date: "7/14/2021" }, { id:2, date: "7/15/2021" } ],
  ];

// iterate over data
const result = data.map(arr => {
  // get occurences count of current array dates
  const occurences = arr.reduce((map, {date}) => 
    map.set(date, (map.get(data) || 0) + 1)
  , new Map);
  // return list of occurences for dateArray elements
  return dateArray.map(date => occurences.get(date) || 0);
}, []);

console.log(result);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

You can do something like this

const dateArray = ["7/12/2021", "7/13/2021", "7/14/2021", "7/15/2021"];

const data = [
  [{
    id: 1,
    date: "7/13/2021"
  }, {
    id: 2,
    date: "7/15/2021"
  }],
  [{
    id: 1,
    date: "7/14/2021"
  }, {
    id: 2,
    date: "7/15/2021"
  }],
];

const result = data.map(item => {

  const resArr = (new Array(dateArray.length)).fill(0);
  item.forEach(entity => {
    const index = dateArray.findIndex(dateItem => dateItem == entity.date);
    if (index >= 0) {
      resArr[index]++;
    }
  });
  return resArr;
});

console.log(result);
Ameer
  • 1,980
  • 1
  • 12
  • 24
  • I am sorry but what does ``` (new Array(dateArray.length)).fill(0); ``` do? and thank you for the quick answer! – Thorai219 Jul 11 '21 at 19:30
  • That creates an array with length of `dateArray` and fills it with 0s. I.e., let's say dateArray has a length of 4, it creates this array `[0, 0, 0, 0]` – Ameer Jul 11 '21 at 19:32