2

I am new to javascript and I am facing issue with complex structure with I need to break into array.

my structure is

   [
    {
        "Key": "ProducePRINKA0370001",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370001"
        }
    },
    {
        "Key": "ProducePRINKA0370038",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370038"
        }
    },
    {
        "Key": "ProducePRINKA0370050",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370050"
          
        }
    }
]

Which I need to break into array and its should look below

 [
    {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370051"
    },
    
        {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370038"
    }
    
    
]

I have tried all the way however couldn't do it. Please help me on this

thanks in advance

2 Answers2

5

You could do it easily using map() as follows -

const myArr = [{
    "Key": "ProducePRINKA0370001",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370001"
    }
  },
  {
    "Key": "ProducePRINKA0370038",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370038"
    }
  },
  {
    "Key": "ProducePRINKA0370050",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370050"

    }
  }
];

const modifiedArr = myArr.map((el) => {
  return el.Record;
})

console.log(modifiedArr)

The above code just maps over your initial array (myArr in this case) and just returns the Record part from each of the objects in the myArr array. Finally what map() built-in function of JS does is that it returns an array consisting of all elements which you return in the function provided inside map.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
3

You can use map function to get the result you want.

var result = source.map((item) => item["Record"]);

console.log(result);

Here source is the array of the source array.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39