-3

I have this array obejcts

  var customers =
[{"id":"1", "name":"John","position":"CEO","office":"NY", "active": "1"},
{"id":"2", "name":"Michael","position":"CEO","office":"NY", "active": "1"},
{"id":"3", "name":"Wanda","position":"CEO","office":"NY", "active": "0"},
{"id":"3", "name":"Novak","position":"CEO","office":"NY", "active": "0"}];

What i need is to get number of customers in that array,

 var indexID = 4;

And also a new array that will look like this

var newArray =
    [{"id", "name","position","office"}];

This new array will only collect title just once and with out active

Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142

2 Answers2

4

First of all json is not valid. it should be:

var customers =
[{"id":"1", "name":"John","position":"CEO","office":"NY", "active": "1"},
{"id":"2", "name":"Michael","position":"CEO","office":"NY", "active": "1"},
{"id":"3", "name":"Wanda","position":"CEO","office":"NY", "active": "0"},
{"id":"3", "name":"Novak","position":"CEO","office":"NY", "active": "0"}];

you can get count using length this way:

var count = customers.length;

and you can create new array with desired properties only using map():

var newArray = customers.map(function(item,index){

    return { "id": item.id, "name": item.name,"position": item.position }

});

WORKING EXAMPLE:

http://jsfiddle.net/abu209tb/

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

Im pretty sure .length is what you are looking for..

var indexID = customers.length;
var newArray = Object.keys(customers[0]); 

thanks goes to this link for reference material: How do I enumerate the properties of a JavaScript object?

Community
  • 1
  • 1
user230910
  • 2,353
  • 2
  • 28
  • 50