-2

how to make new array of the date and time which not picked on the date which available for this case ?

and the case is something like this

const theNumOne = [
    {
      date: "monday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "tuesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    }
]

const pickOne = [
    {
      date: "monday",
      time: ["09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00"]
    }
]

the result would be like this

[
 {
  date: "monday",
  time: ["06.00", "07.00", "08.00",]
 },
  {
   date: "tuesday",
   time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
  }
 {
  date: "wednesday",
  time: ["09.00", "10.00"]
 }
]

the result is the time which not includes on theNumOne in pickOne, like say i pick monday on 09.00 and 10.00 but i did not pic the time from 6- 8 so i want put that 6-8 and the day into that array of object

i have tried this

const result = []
for( let i of theNumOne){
  for(let j of i.time){
    for(let z of pickOne){
      for(let x of z.time){
        if(i !== z ){
          result.push({
            date: i,
            time: z
          })
        }
      }
    }
  }
}
D X
  • 21
  • 5

5 Answers5

1
const theNumOne = [
    {
        date: "monday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
        date: "tuesday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
        date: "wednesday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    }
];

const pickOne = [
    {
        date: "monday",
        time: ["09.00", "10.00"]
    },
    {
        date: "wednesday",
        time: ["06.00", "07.00", "08.00", "09.00"]
    }
];

// get  theNumOne date and time,  pickOne date and time, this function iterates the theNumOne array get's the
// first record, iterates the pickOne array and founds it's date key match,
// sends the theNumOne date and time, and the matched record from pickOne, to remove the unwanted time


const iterateArray = (theNumOne, pickOne) => {

    theNumOne.map((pointer, index) => {

        const extractedRecord = pickOne.find( key => key.date === pointer.date );
        purifyingArray(theNumOne[index], extractedRecord);

    })


};

const purifyingArray = (theNumOne, extractedRecord) => {

    if(extractedRecord !== undefined) {

        extractedRecord.time.map((pointer) => {

            let index = theNumOne.time.indexOf(pointer);

            if(index > -1) {
                theNumOne.time.splice(index,1);
            }
    });

}
console.log(theNumOne);


};


iterateArray(theNumOne, pickOne);
YellowAfterlife
  • 2,967
  • 1
  • 16
  • 24
Klienblat Moshe
  • 322
  • 1
  • 6
0

The filtering of times within an object can be achieved by using a combination of map and filter.

The following solution loops through all the provided days, and filters the time array. It searches for the day key in the pickOne array, if it was found the time array is used to check the time values from theNumOne

Possible solution:

const theNumOne = [
    {
      date: "monday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "tuesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    }
]

const pickOne = [
    {
      date: "monday",
      time: ["09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00"]
    }
];

const results = theNumOne.map(day => ({
    ...day,
    time: day.time.filter(time => {
        const dayFilter = pickOne.find(pickOneDay => pickOneDay.date === day.date);
        return dayFilter === undefined ? true : dayFilter.time.indexOf(time) === -1 
    })
}))

console.log(results)
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
0

This solution is much more efficient then what you did:

I defined a function to find the difference between two arrays (credit to this answer, I just modified it a little), and just reassigned time.

const theNumOne = [{
    date: "monday",
    time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
  },
  {
    date: "tuesday",
    time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
  },
  {
    date: "wednesday",
    time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
  }
];

const pickOne = [{
    date: "monday",
    time: ["09.00", "10.00"]
  },
  {
    date: "wednesday",
    time: ["06.00", "07.00", "08.00"]
  }
];

function diff(a, b) {
  return a.filter(function(i) {
    return b.indexOf(i) < 0;
  });
};
let results = [];
for (i in theNumOne) {
  results.push(JSON.parse(JSON.stringify(theNumOne[i])));
  for (j in pickOne) {
    if (results[i].date == pickOne[j].date)
      results[i].time = diff(results[i].time, pickOne[j].time);
  }
}

console.log(results)
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
  • The usage of `JSON.parse` and `JSON.stringify` to copy an object is kinda hacky. Object Spread has been introduced to cleanly and performantly copy an object. – MaartenDev Aug 18 '19 at 15:48
0

Try This!!!

 const theNumOne = [
    {
        date: "monday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
        date: "tuesday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
        date: "wednesday",
        time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    }
];

const pickOne = [
    {
        date: "monday",
        time: ["09.00", "10.00"]
    },
    {
        date: "wednesday",
        time: ["06.00", "07.00", "08.00"]
    }
];
let ResultArray =theNumOne ;

ResultArray.forEach((theNumOneElement,i) => {
    pickOne.forEach(pickOneElement => {
        if (theNumOneElement.date === pickOneElement.date) {
            ResultArray[i].time = theNumOneElement.time.filter(val => !pickOneElement.time.includes(val));
            //array1 = array1.filter(val => !array2.includes(val));
        }
    });
});
console.log(ResultArray);
Bivin Vinod
  • 2,210
  • 1
  • 12
  • 15
0

This one should do the trick:

const theNumOne = [
    {
      date: "monday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "tuesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
    }
];

const pickOne = [
    {
      date: "monday",
      time: ["09.00", "10.00"]
    },
    {
      date: "wednesday",
      time: ["06.00", "07.00", "08.00", "09.00"]
    }
];
var result = [];
found_dt = false;
for (var el_opt in theNumOne) {
   found_dt=false
   for (var el_pick in pickOne) {
      if(pickOne[el_pick].date == theNumOne[el_opt].date) {
         result.push({date: theNumOne[el_opt].date, time: theNumOne[el_opt].time.filter(x => !pickOne[el_pick].time.includes(x))});
         found_dt=true
      }
   }
   if(!found_dt) {
      result.push({date: theNumOne[el_opt].date, time: theNumOne[el_opt].time})
   }
}

document.write(JSON.stringify(result));
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34