-2

i am working on angular 2 application, while i am trying to find the difference from 2 arrays(last seven days and missing date with in last seven days). whenever the array initialized through the string its working fine like example code 1. but while getting data from database it doesn't work.

var array1 = ['20180605', '20180606', '20180607', '20180608', '20180609', '20180610', '20180611']
var array2 = ['20180606', '20180607', '20180608']
var ind

for (var i = 0; i < array2.length; i++) {
  ind = array1.indexOf(array2[i])
  if (ind > -1) {
    array1.splice(ind, 1)
  }
}
console.log('diff', array1)

but this method is not working

let datas = [
  {'dateString': '20180607'},
  {'dateString': '20180606'},
  {'dateString': '20180608'}
]

let originalDataArray = []
for (let data of datas) {
  originalDataArray.push(data.dateString)
}

let dataArray = []

function formatDate (subtractDate) {
  let datestring
  datestring = moment().subtract(6 - subtractDate, 'days').format('YYYY' + 'MM' + 'DD')
  dataArray.push(datestring)
}

let lastSevenDaysArray = []
for (let i = 0; i < 7; i++) {
  let date = formatDate(i)
}

var array1 = originalDataArray
var array2 = dataArray

var ind

for (var i = 0; i < array2.length; i++) {
  ind = array1.indexOf(array2[i])
  if (ind > -1) {
    array1.splice(ind, 1)
  }
}

console.log('diff', array1)
kiddorails
  • 12,961
  • 2
  • 32
  • 41
Sudhan
  • 11
  • 3
  • You might consider using sensible indentation - it'll help you a lot while debugging. Better readability results in less time needed to understand and fix code – CertainPerformance Jun 12 '18 at 05:56
  • Inspired from [this post](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript): `array1.filter(value => -1 === array2.indexOf(value));` – Adelin Jun 12 '18 at 06:04
  • "@Adelin even shorter, `array1.filter(value => !array2.includes(value))`" (Thank you for the correction but I couldn't update my comment ...) –  Jun 12 '18 at 07:04

2 Answers2

0

You have mixed up array1 and array2. The correct code will be as follows

for (var i = 0; i < array1.length; i++) {
  ind = array2.indexOf(array1[i])
  if (ind > -1) {
     array2.splice(ind, 1)
  }
}
console.log('diff', array1);

Also, I would suggest you to improve your code to following

let datas = [{'dateString': '20180607'},{'dateString': '20180606'},{'dateString': '20180608'}];

let originalDataArray = datas.map(({dateString}) => dateString);

let dataArray = []
for (let i = 0; i < 7; i++) {
  dataArray.push(formatDate(i));
}

function formatDate (subtractDate) {
  return moment().subtract(6 - subtractDate, 'days').format('YYYY' + 'MM' + 'DD')
}
dataArray = dataArray.filter(c => !originalDataArray.includes(c));

console.log('diff', dataArray)
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0

If you have two arrays and one is shorter, there's a simpler solution to find the difference :

const array1 = ['20180605', '20180606', '20180607', '20180608', '20180609', '20180610', '20180611'];
const array2 = ['20180606', '20180607', '20180608'];

for (let el of array2) {
  if (array1.includes(el)) {
    array1.splice(array1.indexOf(el), 1);
  }
}

console.log(array1);

This removes the elements from array2 in array1, leaving you only with element that are not in array2.