Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用旧数组中的两列创建新数组_Javascript_Arrays_Object_Ecmascript 6 - Fatal编程技术网

Javascript 使用旧数组中的两列创建新数组

Javascript 使用旧数组中的两列创建新数组,javascript,arrays,object,ecmascript-6,Javascript,Arrays,Object,Ecmascript 6,我有一个数组,如下所示: items = [ {"year": 2010, "month": 11, "day":23} {"year": 2009, "month": 10, "day":15} {"year": 2009, "month": 10, "day":10} //added after my edit below ] 我想创建一个新数组,其中只有两列,如下所示: newArarry = [ {"year": 2010, "month": 11} {"year": 2009, "m

我有一个数组,如下所示:

items = [
{"year": 2010, "month": 11, "day":23}
{"year": 2009, "month": 10, "day":15}
{"year": 2009, "month": 10, "day":10} //added after my edit below
]
我想创建一个新数组,其中只有两列,如下所示:

newArarry = [
{"year": 2010, "month": 11}
{"year": 2009, "month": 10}
]
现在我正在尝试使用.map(),但它不起作用:

const newArray = [];
newArray.map({
    year: items.year,
    month: items.month
});
编辑

在遵循下面的一个答案之后,我意识到我忘了提到我还需要过滤结果,使其仅为唯一的行。现在我只选择了年份和月份列,我得到了多个重复的行

现在我正在尝试使用.map(),但它不起作用

  • 因为newArray的长度
    0
    ,您正试图映射到它
  • map接受回调,但您正在传递对象
  • 不将映射的输出分配给任何变量
let items=[{“年”:2010,“月”:11,“日”:23},{“年”:2009,“月”:10,“日”:15}]
让final=items.map({day,…rest})=>({…rest}))
console.log(final)
数组的
.map()
方法接受回调,其返回值将用于构造新数组。错误地将对象而不是函数传递给它。要解决此问题,您可以使用
.map()
和一些对象分解:

const项=[
{“年”:2010,“月”:11,“日”:23},
{“年”:2009,“月”:10,“日”:15}
];
const result=items.map({year,month})=>({year,month}));
控制台日志(结果)
。由于控制台包装器{max height:100%!important;top:0;}
Array.map()
需要回调作为第一个参数,因此还必须在原始数组上调用它-否则,实际上是在空数组上迭代

var newArray = items.map(function (item) {
    return {
        year: item.year,
        month: item.month
    };
});

更新

为了确保数组只包含唯一值,而不是映射和筛选,您可以执行以下类似操作:

var items = [
    {year: 2010, month: 11, day: 23},
    {year: 2009, month: 10, day: 15},
    {year: 2009, month: 10, day: 10}
];

var newArray = [];

items.forEach(function (item) {
    var index = newArray.findIndex(function (newItem) {
        return newItem.year === item.year && newItem.month === item.month;
    });

    if (index === -1) {
        newArray.push({ year: item.year, month: item.month });
    }
});

console.log(newArray); // [{ year: 2010, month: 11 }, { year: 2009, month: 10 }]
您的呼叫不正确:

  • 您需要调用源数组:
    items
  • 您需要提供一个函数作为参数
  • Array#map
    返回一个新数组,该数组是针对原始数组的每个成员执行函数的结果
    项目=[
    {“年”:2010,“月”:11,“日”:23},
    {“年”:2009,“月”:10,“日”:15}
    ]
    const newArray=items.map(item=>({
    年份:item.year,
    月份:item.month
    }));
    
    console.log(newArray)
    更正您的items数组,具有多个元素/值的数组必须用逗号分隔

    var items=[{“年”:2010,“月”:11,“日”:23},
    {“年”:2009,“月”:10,“日”:15}]
    常量newArray=[];
    items.map(i=>{
    push({year:i.year,month:i.month});
    });
    
    log(newArray)您没有正确使用
    map
    -您还可以通过rest、spread和destructuring来简化:

    const items=[{“年”:2010,“月”:11,“日”:23},{“年”:2009,“月”:10,“日”:15}];
    const newArray=items.map({day,…res})=>({…res}));
    log(newArray)
    
    .as console wrapper{max height:100%!important;top:auto;}
    对项目进行映射,它将返回新数组。@MennyMez查看我的更新答案以说明您的编辑。谢谢,我目前正在使用此方法。但是,我现在得到了重复的行。有没有办法只选择唯一的行(我的项目数组比我问题中的要大得多)@Mennyg看一看