Suman Verma
Suman Verma

Reputation: 55

Fetch value from array of an objects

I am new to coding.Please be kind to answer :)

I have an array of objects.

[ { UPDATED_BY: '2301132411' },
  { UPDATED_BY: '1432007924' },
  { UPDATED_BY: '973551993' },
  { UPDATED_BY: '1082191138' },
  { UPDATED_BY: '1079399273' } ]

I want the output in below format: ('2301132411','1432007924','973551993','1082191138','1079399273')

I was able to output one value by using below command.I want to use loop here to fetch all the values.Please help with the exact code.Thanks in advance.

var k={};
var arr = [ { UPDATED_BY: '2301132411' },
  { UPDATED_BY: '1432007924' },
  { UPDATED_BY: '973551993' },
  { UPDATED_BY: '1082191138' },
  { UPDATED_BY: '1079399273' } ];
  k=arr[0];
console.log(k);

output: 1432007924

Upvotes: 0

Views: 64

Answers (3)

Mohammad Usman
Mohammad Usman

Reputation: 39382

If I have understood your question correctly, You can use Array#map function to achieve this as follows:

var arr = [
  { UPDATED_BY: '2301132411' },
  { UPDATED_BY: '1432007924' },
  { UPDATED_BY: '973551993' },
  { UPDATED_BY: '1082191138' },
  { UPDATED_BY: '1079399273' }
];
var k = arr.map(function(obj) {
  return obj['UPDATED_BY'];
});

console.log(k); // If you wants an array of all values
console.log('(' + k + ')'); // In case you wants one complete string

Upvotes: 1

KevBot
KevBot

Reputation: 18908

var arr = [
  {UPDATED_BY: '2301132411'},
  {UPDATED_BY: '1432007924'},
  {UPDATED_BY: '973551993'},
  {UPDATED_BY: '1082191138'},
  {UPDATED_BY: '1079399273'
}]

arr = '(' + arr.map(function (item) {
  return item.UPDATED_BY
}) + ')';

console.log(arr);

Upvotes: 1

Vasan
Vasan

Reputation: 4956

Try this:

var output = [];
for(var i = 0;i < arr.length; i++) {
    output.push(arr[i]['UPDATED_BY']);
}
console.log(output);

Upvotes: 0

Related Questions