1

I have data

{
  YHOO: [
    {
      date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
      open: 25.25,
      high: 43,
      low: 24.5,
      close: 33,
      volume: 408720000,
      adjClose: 1.38,
      symbol: 'YHOO'
    },
    ...
    {
      date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
      open: 35.07,
      high: 35.89,
      low: 34.76,
      close: 35.69,
      volume: 21368600,
      adjClose: 35.69,
      symbol: 'YHOO'
    }
  ],
  GOOGL: [
    {
      date: Thu Aug 19 2004 00:00:00 GMT-0400 (EDT),
      open: 100,
      high: 104.06,
      low: 95.96,
      close: 100.34,
      volume: 22351900,
      adjClose: 100.34,
      symbol: 'GOOGL'
    },
    ...
    {
      date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
      open: 1033.92,
      high: 1039.75,
      low: 1030.35,
      close: 1035.23,
      volume: 1166700,
      adjClose: 1035.23,
      symbol: 'GOOGL'
    }
  ],
  ...
}

How do I skip the keys, so the object becomes an array of objects like this

[
    {
      date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
      open: 25.25,
      high: 43,
      low: 24.5,
      close: 33,
      volume: 408720000,
      adjClose: 1.38,
      symbol: 'YHOO'
    },
    {
      date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
      open: 35.07,
      high: 35.89,
      low: 34.76,
      close: 35.69,
      volume: 21368600,
      adjClose: 35.69,
      symbol: 'YHOO'
    },
    {
      date: Thu Aug 19 2004 00:00:00 GMT-0400 (EDT),
      open: 100,
      high: 104.06,
      low: 95.96,
      close: 100.34,
      volume: 22351900,
      adjClose: 100.34,
      symbol: 'GOOGL'
    },
    {
      date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
      open: 1033.92,
      high: 1039.75,
      low: 1030.35,
      close: 1035.23,
      volume: 1166700,
      adjClose: 1035.23,
      symbol: 'GOOGL'
    }
]
Jamgreen
  • 10,329
  • 29
  • 113
  • 224

3 Answers3

1

You can do this

var arr = {
  YHOO: [
    {
      date: 'Fri Apr 12 1996 00:00:00 GMT-0400 (EDT)',
      open: 25.25,
      high: 43,
      low: 24.5,
      close: 33,
      volume: 408720000,
      adjClose: 1.38,
      symbol: 'YHOO'
    },
    {
      date: 'Thu Nov 14 2013 00:00:00 GMT-0500 (EST)',
      open: 35.07,
      high: 35.89,
      low: 34.76,
      close: 35.69,
      volume: 21368600,
      adjClose: 35.69,
      symbol: 'YHOO'
    }
  ],
  GOOGL: [
    {
      date: 'Thu Aug 19 2004 00:00:00 GMT-0400 (EDT)',
      open: 100,
      high: 104.06,
      low: 95.96,
      close: 100.34,
      volume: 22351900,
      adjClose: 100.34,
      symbol: 'GOOGL'
    },
    {
      date: 'Thu Nov 14 2013 00:00:00 GMT-0500 (EST)',
      open: 1033.92,
      high: 1039.75,
      low: 1030.35,
      close: 1035.23,
      volume: 1166700,
      adjClose: 1035.23,
      symbol: 'GOOGL'
    }
  ],
};

var newArr = [];

for(var item in arr){
  if(arr.hasOwnProperty(item)){        
    arr[item].forEach(x => newArr.push(x));        
  }
}

console.log(newArr);

And from 4castle's comment you can also do

for(var item in arr){
  if(arr.hasOwnProperty(item)){        
    newArr = newArr.concat(arr[item]);       
 }
}
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

This can be achieved by iterating over each property of the data array such as.

var data2 = [];
Object.keys(data).forEach(key => data2 = data2.concat(data[key]));
console.log(data2);

This will result with a singular array of values .

Nico
  • 12,493
  • 5
  • 42
  • 62
  • I have tried `Object.keys(data).forEach(key => data.concat(data[key]))` since I am trying to solve it in 1 line and without using another variable, but it says `TypeError: data.concat is not a function` – Jamgreen Aug 15 '16 at 06:57
0

First of all, There is something wrong in your code.You have data property in every object, but the value of it is illegal,you can modify them as string.

{
      //data should be string type like this
      //otherwise the browser will throw an error
      date: 'Fri Apr 12 1996 00:00:00 GMT-0400 (EDT)',
      open: 25.25,
      high: 43,
      low: 24.5,
      close: 33,
      volume: 408720000,
      adjClose: 1.38,
      symbol: 'YHOO'
}
···

And I think you can use for-in loop to do it.

//data variable is reference to your data.
var newData = [];
for ( var key in data ) {
 for ( var innerKey in data[key] )
  newData.push( data[key][innerKey] );
}

Or using regular expressions to solve this problem.

var finalData = [];

var dataArr = JSON.stringify( data ).match( /\[[^\]]*\]/g );

dataArr.forEach( function( item ) {
 var arr = JSON.parse( item );
 for( var i = 0, len = arr.length; i < len; i++ )
  finalData.push( arr[i] );
});
napoleonQin
  • 53
  • 1
  • 5