Home > Blockchain >  How to get key value in this case {Java Script}
How to get key value in this case {Java Script}

Time:06-24

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

CodePudding user response:

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

const filtered = o.map(i =>  i.aaa)
console.log(filtered)

CodePudding user response:

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)

CodePudding user response:

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

0.aaa = 11

1.bbb = 12

  • Related