0

I want to convert an object with specific properties into array of string containing properties values. For an instance take Object Employee with below properties with values

Employee.Name='XYZ'
Employee.ID=123
Employee.Address='ABC'

I want this all to be in array as

var arr=['XYZ',123,'ABC']

How to iterate over the properties. Is this possible? Please assist here.

0

7 Answers 7

2

Use $.map()

var arr = $.map(Employee, function(value, key){
    return value
})

Demo: Fiddle

Note: The order of loop is not dependable, so the order of values in the array may not be always same


Another way to handle it is to use a fixed array of keys so that the output array will have a predefined sequence

var keys = ['Name', 'ID', 'Address'];
var Employee = {};
Employee.Name = 'XYZ'
Employee.ID = 123
Employee.Address = 'ABC'
var arr = $.map(keys, function (key, idx) {
    return Employee[key]
})
console.log(arr)

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Loop through the object like this

var arr = [];
for (var key in Employee) {
    arr.push(Employee[key]);
}

Note: Order is not defined in this case

4 Comments

+1 but it's best to use var arr = []; rather than var arr = new Array();. Also worth noting that the order is not defined.
Ah i'll update, btw why is arr=[] better the arr=new Array() Is it faster?
@ Anton: "Is it faster?" Usually, but that's not important. Array can be redefined (var Array, for instance), and it's confusing and inconsistent: var a = new Array(5); gives you an array containing no entries with a length of 5. var a = new Array("5"); gives you an array with one entry ("5"). var a = new Array(5, 2); gives you an array with two entries (5 and 2) and a length of 2. In contrast, var a = [5]; gives you an array with one entry and var a = [5, 2]; an array with two entries. Consistent and clear.
@T.J.Crowder Ah i see now why it could be confusing. Thanks for the explanation :)
0

You can loop through each property and add it's value to the array:

var arr = [];

for (var prop in Employee) {
    if (Employee.hasOwnProperty(prop)) {
        arr.push(Employee[prop]);
    }
}

Example - http://jsfiddle.net/infernalbadger/2PHpZ/

Or using jQuery:

var arr = $.map(Employee, function(propValue) {
    return propValue;
});

Example - http://jsfiddle.net/infernalbadger/2PHpZ/1/

Comments

0
var myArray=[];

for (var key in Employee) {
  if (Employee.hasOwnProperty(key)) {
    myArray.push(Employee[key]));
  }
}

1 Comment

what is p in your if?
0
var Employee = {};
Employee.Name='XYZ';
Employee.ID=123;
Employee.Address='ABC';

var empArray =[];

$.each(Employee,function(item){
    empArray.push(item);
});

console.log(empArray);

fiddle

Comments

0

JavaScript since 1.7. Reference to JavaScript language advanced Tips & Tricks.

var arr = [Employee.Name,Employee.ID,Employee.Address];

Comments

0

Object.values() was added to browsers in ES2017 and was designed just for that purpose.

Object.values({Name:'XYZ', ID:123, Address:'ABC'});// ['XYZ',123,'ABC']

If it has to work in old browsers, it's easy to polyfill:

Object.values = Object.values || function values(obj) {
    var keys = Object.keys(Object(obj)), i = keys.length, values = [];
    while (i--) {
        values[i] = obj[keys[i]];
    }
    return values;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.