I am trying to do something complex thing with array. I have array
ORGANIZATIONS = [
{
"MAINID": "INAP1650001",
"Status": "Approved"
},
{
"MAINID": "INAP1650002",
"Status": "Pending"
},
{
"MAINID": "INAP1650003",
"Status": "Approved"
},
{
"MAINID": "INAP1650004",
"Status": "Pending"
},
{
"MAINID": "INAP1650005",
"Status": "Approved"
}
]
So I am trying to get all "MAINID" which status is "Approved" and want to create array of MAINID like MAINID_Array = [INAP1650001, INAP1650003,INAP1650005]
Could anybody help me please as I have tried using below but its give me all MAINID
var status = ''
var MAINID = ''
var MAINID_array = []
let len = ORGANIZATIONS.length
for (var i = 0; i < len; i++) {
Status = ORGANIZATIONS[i].STATUS;
MAINID = ORGANIZATIONS[i].MAINID;
console.log('STATUS', Status);
console.log('MAINID', MAINID);
console.log('Approve STATUS', Status);
MAINID_array.push(MAINID);
}
console.log(MAINID_array)
const MAINID_Array = ORGANIZATIONS.filter(organization => organization['Status'] === 'Approved').map(approvedOrganization => approvedOrganization['MAINID']);but the answer provided by stackoverflow.com/a/64360006/7377537 Derek.W using reduce is the best neat solution.