javascript - Create new array from an existing array attributes -


i trying create new array out of limited values existing array. in example below largearray contains many attributes – year, books, gdp , control. lets want create new array include year , gdp.

var largearray = [     {year:1234, books:1200, gdp:1200, control:1200},      {year:1235, books:1201, gdp:1200, control:1200},      {year:1236, books:1202, gdp:1200, control:1200} ]; 

the new array trying get, this:

var newarray = [     {year:1234, gdp:1200},     {year:1235, gdp:1200},     {year:1236, gdp:1200} ]; 

use $.map()

var largearray = [{year:1234, books:1200, gdp:1200, control:1200}, {year:1235, books:1201, gdp:1200, control:1200}, {year:1236, books:1202, gdp:1200, control:1200}, {year:1237, books:1203, gdp:1200, control:1200}, {year:1238, books:1204, gdp:1200, control:1200}]; var newarray = $.map(largearray, function (value) {     return {         year: value.year,         gdp: value.gdp     } }) 

demo: fiddle


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -