-3

I am learning javascript. I have an array that looks like :

myArray = [{
      "item":{
         "fields":{
            "myfield":"Value1"
         }
      }
   },
   {
      "item":{
         "fields":{
            "myfield":"Value2"
         }
      }
   }];

I want to create a new array with ["Value1","Value2"].

How should I do that ?

Thanks a lot !

hellomomo
  • 11
  • 1
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). What can we expect the data to look like in general? – Sebastian Simon Dec 15 '21 at 16:32

1 Answers1

0

You can use the Javascript function map() for this. :

const myArray = [{
      "item":{
         "fields":{
            "myfield":"Value1"
         }
      }
   },
   {
      "item":{
         "fields":{
            "myfield":"Value2"
         }
      }
   }];

const r = myArray.map(e => e.item.fields.myfield)
console.log(r)
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79