0

I have this array that I want to check the days that has the job done inside.(In this case Monday)

const done = [
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "done"]},
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "done", "done"]},
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "not yet"]},
]

 const freeTime = done
  .filter((event) => event.job === 'done')
console.log(freeTime) // I get nothing here?
6
  • 1
    event.job will never equal to 'done', if you do typeof event.job will return you the type of job as Array, your logical operator is wrong. :)
    – Adrian
    Commented Nov 15, 2017 at 0:38
  • So you want to return 'Monday' because each element has a days array that has a first element of 'Monday' and a job array with the first element is 'done'. Is that right? If so, filter won't work well because you need to look at every element pf the array to know and then you want to return what? the string "Monday"
    – Mark
    Commented Nov 15, 2017 at 0:41
  • It dosn't have to be the first day I want to check which day has done in all 3 objects
    – Jason
    Commented Nov 15, 2017 at 0:42
  • 1
    what are the contents of arr?
    – floor
    Commented Nov 15, 2017 at 0:43
  • mistype it should be done
    – Jason
    Commented Nov 15, 2017 at 0:45

3 Answers 3

1

To check which day has 'done' in all three objects you need to look at them all and build a map that will give you true or false for each day. You can do that like this:

const done = [
    {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "done"]},
    {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "done", "done"]},
    {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "not yet"]},
   ]

var map = done.reduce((a, c ) => {
    c.days.forEach((day, i) => {
        a[day] =  ((a[day] == undefined || a[day] ) && c.job[i] === "done") || false
    })
    return a
}, {})

console.log(map)

This will result in on abject for true or false for each day. Is that what you'r after?

1
  • I actually need only Monday I don't care about other days. Thank!
    – Jason
    Commented Nov 15, 2017 at 1:05
0

First of all, as I said in the comments, event.job is an Array so doing === 'done' will never evaluate to true.

What you need is to apply .every() filter on the array rather than ===.

const freeTime = done.filter((event) => event.job.every(isDone) )

function isDone(status){
  return status == "done";
}

This is of course assuming that all three values in jobs property must be 'done'.

JSFiddle

If your structure of those object is that each value in days corresponds to each value in jobs (i.e. index 0 in days is assigned to index 0 of jobs), then you need to design your objects more efficiently. The current design not efficient nor practical.

3
  • 1
    I don't think this is what the question is looking for. I think the OP wants Monday, because the first element on every job array is done.
    – Mark
    Commented Nov 15, 2017 at 1:00
  • 1
    @Mark_M While you're right, it was/is a guessing game with the details provided. Absolutely nothing about the structure of the objects, just a vague question. Nonetheless, bad structure fundamentally. Those objects should contain objects for each day to avoid complications in the feature, that being said it has nothing to do with us at the end.
    – Adrian
    Commented Nov 15, 2017 at 1:11
  • Agreed -- it's an awkward data structure.
    – Mark
    Commented Nov 15, 2017 at 1:14
0

Here's a functional way of doing it. A bit convoluted, but it gets the job done.

const done = [
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "done"]},
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "done", "done"]},
 {days:["Monday", "Tuesday", "Friday"] ,job: ["done", "not yet", "not yet"]},
]

// Keep days that occur done in all "weeks"
const numWeeks = done.length;

// Loop through all weeks and days to see when they are "done"
const daysObj = done.map((x) => x.job.map((x) => x == "done")
  .map((y, index) => y ? x.days[index] : ""))
  .map((x) => x.filter((day) => day))
  .reduce((acc, curr) => acc.concat(curr))
  .reduce(function(p, c){
    if (c in p) {
      p[c]++;
    } else {
      p[c]=1;
    }
    return p;
  }, {});
console.log(daysObj);
// {Monday: 3, Friday: 2, Tuesday: 1}

// Find days that occur done in all weeks
const result = Object.keys(daysObj)
  .map((x) => daysObj[x] == numWeeks ? x : null)
  .filter((x) => x)
console.log(result);
// ["Monday"]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.