1

I'm trying to create an array of objects with filtered values

// desired output

     [
         { "2017-02-01": "09:00" },
         { "2017-02-02": "09:00" },
     ]

My Js object

let res = {
  "2018-02-01": [
    {
      "time": "09:00",
      "available": true,
    },
    {
      "time": "10:00",
      "available": false,
    }
  ],
  "2018-02-02": [
    {
      "time": "09:00",
      "available": true,
      "reference": null
    },
    {
      "time": "10:00",
      "available": false,
      "reference": null
    }
  ]
}

// My attempt 
output = _.keys(res).map( i => res[i].filter( t => t.available))

console.log(output); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>

3
  • Can you please explain in words what are you trying to achieve?
    – Rajesh
    Commented Apr 6, 2018 at 5:13
  • updated the question :)
    – Deano
    Commented Apr 6, 2018 at 5:15
  • 1
    If you notice, I highlighted in words. What I meant was what is the logic to filter objects? I did understand that you are trying to get the first available timeslot but I had to read your entire code for it.
    – Rajesh
    Commented Apr 6, 2018 at 5:23

4 Answers 4

3

try this:

let res = {
  "2018-02-01": [
    {
      time: "09:00",
      available: true
    },
    {
      time: "10:00",
      available: false
    }
  ],
  "2018-02-02": [
    {
      time: "09:00",
      available: true,
      reference: null
    },
    {
      time: "10:00",
      available: false,
      reference: null
    }
  ]
};
let output = Reflect.ownKeys(res).map(key => ({
  [key]: res[key].filter(obj => obj.available)[0].time
}));
console.log(output);

your logic is basically correct with a little error because you didn't take a good control of your return value in map function.

And that is what i did:

[key]: /*basically  your code*/[0].time
6
  • 1
    @Rajesh, First Reflect is pure JavaScript concept. Look here : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 6, 2018 at 5:25
  • @Rajesh, Second, it is very straight forward, what explanation you need? Commented Apr 6, 2018 at 5:26
  • @Rajesh, then instead of commenting sarcastically, better you should show some maturity and simply comment Your code also looks good, better to add some explanation that's it. Commented Apr 6, 2018 at 5:38
  • Also thanks for the link. Learned something new. For readers, do check browser compatibility before using
    – Rajesh
    Commented Apr 6, 2018 at 5:39
  • @Rajesh, if it was so then why did you delete your the very first comment? Anyways, please don't feel offended. Everyday we learn something new not only technical but other stuff as well :) Commented Apr 6, 2018 at 5:47
1

If I understand correct, what you need is the first timeslot that is available.

For this, you can loop over keys of response. Post that you can use array.find or _.find and get first value that is available.

Now create an object with date value as key and value as object.time

let res = {
  "2018-02-01": [{
      "time": "09:00",
      "available": true,
    },
    {
      "time": "10:00",
      "available": false,
    }
  ],
  "2018-02-02": [{
      "time": "09:00",
      "available": true,
      "reference": null
    },
    {
      "time": "10:00",
      "available": false,
      "reference": null
    }
  ]
}

// My attempt 
output = Object.keys(res).reduce(function(acc, key) {
  const obj = res[key].find(x => !!x.available);
  if (!!obj)
    acc.push({
      [key]: obj.time
    });
  return acc;
}, [])

console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>

1

Iterate through the res object using for in loop. Find the object which has available as true using Array.find method. Store key/value in an object and push the object in an array.

let res = {
  "2018-02-01": [
    {
      "time": "09:00",
      "available": true,
    },
    {
      "time": "10:00",
      "available": false,
    }
  ],
  "2018-02-02": [
    {
      "time": "09:00",
      "available": true,
      "reference": null
    },
    {
      "time": "10:00",
      "available": false,
      "reference": null
    }
  ]
}

var arr = [];

for (var date in res) {
  arr.push({
    [date]: res[date].find(r => r.available).time
  });
}

console.log(arr);

0

If you want to do using native javascript then you can use this code -

var arr = [];
for (const [key, value] of Object.entries(res)) {
    var obj = {};

    for (const o of value) {

        if (o.available) { obj[key] = o.time; }
    } 

    arr.push(obj);
}

console.log(arr);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.