从现有数组属性创建新数组

Roh*_*eep 4 javascript arrays jquery

我正在尝试使用现有数组中的有限值创建一个新数组。下面的示例LargeArray包含许多属性 - 年份、书籍、gdp 和控制。假设我想创建一个仅包含年份和 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}
];
Run Code Online (Sandbox Code Playgroud)

我试图获取的新数组如下所示:

var NewArray = [
    {year:1234, gdp:1200},
    {year:1235, gdp:1200},
    {year:1236, gdp:1200}
];
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 5

使用$.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
    }
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴