Home > Mobile >  how to get user_name from the title that has "eJx2" in it from this json array?
how to get user_name from the title that has "eJx2" in it from this json array?

Time:05-26

I wanna get user_name from one of these that have "eJx2" in it. I have this code but this code prints all of "user_name"s. I want to just get the user_name of those that have "eJx2" in it.

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);
    }
}

and this is the 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'
  }
}

CodePudding user response:

Use filter() to filter title have 'eJx2' and map() to get only 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)

CodePudding user response:

Use array filter

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)

  • Related