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
Post a Comment