skip to Main Content

I need extract data (array.tags.name) from all fields and put them to result array.

screenshot JavaScript console

3

Answers


  1. Just loop through with nested loop.

    const result = [];
    
    for (const item of array) {
      const tags = item.tags;
      for (const tag of tags) {
        result.push(tag.name);
      }
    }
    
    console.log(result);
    
    Login or Signup to reply.
  2. Here the code to get arrays.tags.name. I have tested this with your given data as well.

    const data = [  {     
        tags: [   
        { name: 'LandingPage', value: 'true' },     
        { name: 'Country', value: 'GB' },     
        { name: 'Carrier', value: 'Evri' },     
        { name: 'EventCode', value: 'Dispatched' }    
        ]
      },
    ];
    const results = [];
    
    for (let column of data) {
      for (let tag of column.tags) {
        results.push(tag.name);
      }
    }
    console.log(results);
    Login or Signup to reply.
  3. Solution using map.

    const data = [
      {
        tags: [
          { name: 'LandingPage', value: 'true' },
          { name: 'Country', value: 'GB' },
          { name: 'Carrier', value: 'Evri' },
          { name: 'EventCode', value: 'Dispatched' }
        ]
      }
    ];
    
    const results = data.map(item => item.tags.map(tag => tag.name)).flat();
    
    console.log(results);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search