I have an object and want to get userName.
const example = [
{
id: 793,
name: 'John',
weight: '66',
data: [
{
id: 793,
userName: 'John Ferny',
},
],
},
];
I'm not sure that example.data.filter((item) => item === item.userName is correct
Directly:
const userName = example[0].data[0].userName;
If more than one userName:
const userNames = example.flatMap(({data}) => data.map(({userName})=> userName));
To FIND the first item with userName === "John Ferny"
const item = example.filter(item => item.data.find(({userName}) => userName === user))
const example = [{ id: 793, name: 'John', weight: '66', data: [ { id: 793, userName: 'John Ferny',}, ], },{ id: 794, name: 'Fred', weight: '66', data: [ { id: 794, userName: 'Fred Ferny',}, ], },];
// directly
console.log(example[0].data[0].userName)
// If more than one:
const userNames = example.flatMap(({data}) => data.map(({userName})=> userName))
console.log(userNames)
// To find an object with userName === something
const user = "John Ferny"
const item = example.filter(item => item.data.find(({userName}) => userName === user))
console.log(item)