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? Commented Apr 6, 2018 at 5:13
  • updated the question :) 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. 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
Sign up to request clarification or add additional context in comments.

6 Comments

@Rajesh, First Reflect is pure JavaScript concept. Look here : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@Rajesh, Second, it is very straight forward, what explanation you need?
@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.
Also thanks for the link. Learned something new. For readers, do check browser compatibility before using
@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 :)
|
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>

Comments

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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.