Простой способ?
Возможно, вы можете использовать includes()
для строкового массива:
const data = [
{
title: 'New Posts',
data: [
{
username: 'firstnamefromnewpost',
content: 'this is some content that will go on forever and ever',
},
{
username: 'name',
content:
'lost content goals lets',
},
{
username: 'another name',
content:
'lost content goals lets start with some',
},
],
},
{
title: 'Different Posts',
data: [
{
username: 'usernametag',
content: 'this is some content that will go on forever and ever',
},
{
username: 'name',
content:
'lost content goals lets start',
},
],
},
];
var isExists = JSON.stringify(data).includes('firstnamefromnewpost');
console.log(isExists);
ИЛИ: Если вы хотите проверить свойство username во вложенном массиве data :
const data = [
{
title: 'New Posts',
data: [
{
username: 'firstnamefromnewpost',
content: 'this is some content that will go on forever and ever',
},
{
username: 'name',
content:
'lost content goals lets',
},
{
username: 'another name',
content:
'lost content goals lets start with some',
},
],
},
{
title: 'Different Posts',
data: [
{
username: 'usernametag',
content: 'this is some content that will go on forever and ever',
},
{
username: 'name',
content:
'lost content goals lets start',
},
],
},
];
function isExists(data, userName){
for(const i of data){
if(i.data.map(n => n.username).includes(userName)) return true
}
return false;
}
console.log(isExists(data, 'firstnamefromnewpost'));