-1

Desired output will be like, I want to find last and first index. I'm having trouble in handling this. I'm not be able to get the index from this objects.

  {
        '13-2-2022' =>[
                {
                "name":"abc"
                "date":"13-2-2022"
                },
                {
                "name":"xyz"
                "date":"13-2-2022"
                }
        ]
        '15-2-2022' =>[
                {
                "name":"azx"
                "date":"15-2-2022"
                },
                {
                "name":"qwe"
                "date":"15-2-2022"
                }
        ]
        '16-2-2022' =>[
                {
                "name":"azx"
                "date":"16-2-2022"
                },
                {
                "name":"qwe"
                "date":"16-2-2022"
                }
                {
                "name":"qpe"
                "date":"16-2-2022"
                }
        ]
    }
Dev
  • 55
  • 6
  • Please create a [mcve] by posting compilable code. Is this a normal object or a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object? `=>` is confusing here. – adiga Oct 10 '22 at 14:05
  • Also, if it is an object, there are plenty of questions regarding this: [Get the first key name of a JavaScript object](https://stackoverflow.com/questions/3298477) and [getting the last item in a javascript object](https://stackoverflow.com/questions/4317456) – adiga Oct 10 '22 at 14:06
  • It is a map object actually. – Dev Oct 10 '22 at 14:29
  • `const keys = Array.from(map.keys()), first = keys[0], last = keys[map.size-1]` – adiga Oct 10 '22 at 14:37
  • Duplicate of [First item from a Map on JavaScript ES2015](https://stackoverflow.com/questions/32373301) and [How to get a last element of ES6 Map without iterations?](https://stackoverflow.com/questions/30921283) – adiga Oct 10 '22 at 14:37
  • I want to get object from every date accordingly. For example `16-2-2022`, its last and first object similarly `15-2-2022` it's first and last object, similarly for others as well. – Dev Oct 10 '22 at 16:13
  • Use `Array.from(map.values())` instead and get the array indices accordingly. `const firstValues = Array.from(map.values(), (val) => val[0])` and `const lastValues = Array.from(map.values(), (val) => val[val.length-1])` – adiga Oct 11 '22 at 06:11

1 Answers1

0

You can get the object keys with Object.keys function.

const keys = Object.keys(myMap);
const first = keys[0];
const last = keys[keys.length-1];
Clem
  • 2,150
  • 15
  • 17