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'
    }
]
5
  • Would it be data = Object.keys(data).map((key) => data[key]); Commented Aug 15, 2016 at 6:34
  • Yes, then flatten that 2D array using data = [].concat(...data); Commented Aug 15, 2016 at 6:35
  • In ES7 you will be able to do data = [].concat(...Object.values(data)); Commented Aug 15, 2016 at 6:43
  • So I get the new array with return [].concat(Object.keys(data).map((key) => data[key]));? Commented Aug 15, 2016 at 6:53
  • The spread operator ... is required. See stackoverflow.com/a/34980419/5743988 Commented Aug 15, 2016 at 12:47

3 Answers 3

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]);       
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No need for the forEach and push. Just do newArr = newArr.concat(arr[item]).
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 .

1 Comment

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
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] );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.