1

I have a data file that I'm reading into an array. The array created by the original data file looks like this:

var originalArray = [ {vendor: 2001, bananas: 50, apples:75, oranges: 12},
                      {vendor: 2002, bananas: 25, apples:60, oranges: 82},
                      {vendor: 2003, bananas: 36, apples:41, oranges: 73},
                      {vendor: 2004, bananas: 59, apples:62, oranges: 87}];

I select a vendor from the array using .filter (this is working fine) but then I need to change the new array (the one that contains only one vendor code) into an array that looks like this.

var desiredArray = [ {fruitName: "bananas", qty: 50},
                     {fruitName: "apples", qty: 75 },
                     {fruitName: "oranges", qty: 12} ];

Using .push I can get the quantities, but how can I get the fruit names to go from field names to values in the "fruitName" field?

DG_Montana
  • 63
  • 1
  • 2
  • 4
  • Show the code you are using with `.push`, it is probably a simple fix. – Matt Dodge Mar 25 '13 at 16:24
  • I'm filling the result array using .push as follows. However, it doesn't give me the field names and the result array contains only one element with three values, rather than three elements with one value each. `var desiredVendor = originalArray.filter(matchVendor); var result = []; for(var i = 0; i < desiredVendor.length; i++){ result.push(desiredVendor[i].bananas); result.push(desiredVendor[i].apples); result.push(desiredVendor[i].oranges); }` – DG_Montana Mar 25 '13 at 16:33
  • See my answer below, based on your comment here you'd set selectedVendor like this `var selectedVendor = desiredVendor[0];` and then use the `in` loop like the example instead of using the index based for loop you're using now. – clav Mar 25 '13 at 16:47

5 Answers5

6

If you've got the selected vendor object, you can do something like this to create the desiredArray.

var desiredArray = [];
var selectedVendor = originalArray[2]; // this is the vendor you got via .filter

for(property in selectedVendor) {
    if(property !== 'vendor') {
        desiredArray.push({fruitName: property, qty: selectedVendor[property]});
    }
}
clav
  • 4,221
  • 30
  • 43
  • Thanks - this did the trick with just one small edit. I had to access the [0] element of `originalArray` rather than the [2] element. Thanks for the quick response! – DG_Montana Mar 25 '13 at 16:52
1

Try this

var vendorId = 2001;

var desiredArray = originalArray.filter( function( v ) {

    return v.vendor == vendorId ;

}).map( function( v ){

    var s = [];

    for ( var i in v ){

        if ( i == 'vendor' ) continue;

        s.push( {fruitName: i , qty: v[i] } );            
    }
    return s;

}).pop(); 

The desiredArray has value

[ {fruitName: "bananas", qty: 50},
  {fruitName: "apples", qty: 75 },
  {fruitName: "oranges", qty: 12} ]
rab
  • 4,134
  • 1
  • 29
  • 42
1

Use the for...in loop:

    var currentVendor;
    var desiredArray = [];

    //ok, this is only an example
    currentVendor = {vendor: 2001, bananas: 50, apples:75, oranges: 12};
    for (var prop in currentVendor) {
        if(prop!='vendor')
            desiredArray.push({fruitName: prop, qty: currentVendor[prop]});
    }
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
1
   var a = [{vendor:2001,apple :50,orange:20},{vendor:2002,apple:50, orange:10}];

   var matchedVendor = 2001;

For Filter

    var filteredArray = [];
    for (var i=0; i < a.length; i++){
    if(a[i].vendor === matchedVendor){
     filteredArray = a[i];
          break;
     }}


    var desiredArray = [];

    for(prop in filteredArray){
    if(prop !== "vendor"){
        desiredArray.push({fruitName:prop,qty:filteredArray[prop]});
    }
 }
karthick
  • 5,998
  • 12
  • 52
  • 90
0
var fruit = {};
for (var i = 0; i < originalArray.length; i++) {
  for (var key in originalArray[i]) {
    if (key != selectedVendor)
      continue;
    if (!fruit[key])
      fruit[key] = 0;
    fruit[key] += originalArray[i][key];
  }
}
var desiredArray = [];
for (var fruitName in fruit) {
  desiredArray.push({fruit: fruitName, qty: fruit[fruitName]});
}
Stuart M
  • 11,458
  • 6
  • 45
  • 59