Quiero obtener el nombre de usuario de uno de estos que tiene "eJx2". Tengo este código, pero este código imprime todos los "nombres de usuario". Solo quiero obtener el nombre de usuario de aquellos que tienen "eJx2".
var titles = arry.data.map(el => el.title); for (var k in titles) { var never = titles[k].includes('eJx2'); if (never) { var names = arry.data.map(el => el.user_name); console.log(names); } }y este es el json:
{ data: [ { id: '46475273517', user_name: 'testtwo', title: 'Hello this is my test for the eJx2', is_set: true }, { id: '46471542013', user_name: 'testone', title: 'Hello this is my test for the eJx3', is_set: false }, { id: '46474254233', user_name: 'testthree', title: 'Hello this is my test for the eJx7', is_set: false } ], pagination: { cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0' } }Usar filtro de matriz
array.data.filter(item => item.title.includes("eJx2")) var array = { data: [ { id: '46475273517', user_name: 'testtwo', title: 'Hello this is my test for the eJx2', is_set: true }, { id: '46471542013', user_name: 'testone', title: 'Hello this is my test for the eJx3', is_set: false }, { id: '46474254233', user_name: 'testthree', title: 'Hello this is my test for the eJx7', is_set: false } ], pagination: { cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0' } } var filteredData = array.data.filter(item => item.title.includes("eJx2")) console.log(filteredData)Use filter() para filtrar el título, tenga 'eJx2' y map() para obtener solo nombre de user_name :
const arry = { data: [ { id: '46475273517', user_name: 'testtwo', title: 'Hello this is my test for the eJx2', is_set: true }, { id: '46471542013', user_name: 'testone', title: 'Hello this is my test for the eJx3', is_set: false }, { id: '46474254233', user_name: 'testthree', title: 'Hello this is my test for the eJx7', is_set: false } ], pagination: { cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0' } } const result = arry.data.filter(({title}) => title.includes("eJx2")).map(el => el.user_name) console.log(result)