-2

Hi so what iam trying to achieve is basically is simple.Iam try to get only the object key's value from the given array so this is my example array

[{'aaa': '11','text':'hello'},{'aaa': '12','text':'bye'}] iam expecting a output of something like this [11,12]

where iam getting the data of the aaa key only

  • There is no concept of "the first key` - it could change based on javascript implementations – Jamiec Jun 24 '22 at 08:14
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Don't Panic Jun 24 '22 at 08:18

3 Answers3

2

This will do what you want, but the order could well change between different javascript implementations

const input = [{'aaa': '11','text':'hello'},{'bbb': '12','text':'bye'}]

const output = input.map(x => Object.values(x)[0]);

console.log(output)

I note you updated the quesrtion to have both the same first key - if you're looking for aaa value from all elements this is much simpler

const input = [{'aaa': '11','text':'hello'},{'aaa': '12','text':'bye'}]

const output = input.map(x => x.aaa);

console.log(output)
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

const o = [{'aaa': '11','text':'hello'},{'aaa': '12','text':'bye'}]

const filtered = o.map(i => +i.aaa)
console.log(filtered)
vdegenne
  • 12,272
  • 14
  • 80
  • 106
-1

The JSON path if you convert this into a JavaScript Object Notation format (as JSON).

0.aaa = 11

1.bbb = 12

Sowmo0509
  • 54
  • 7