Remove key from all objects in array

前端 未结 2 1693
时光取名叫无心
时光取名叫无心 2020-12-06 18:24

I have the following array of objects:

[{id:1, value:\"100\", name:\"dog\" ...},
{id:2, value:\"200\", name:\"cat\" ...},
{id:3, value:\"300\", name:\"fish\"         


        
2条回答
  •  醉酒成梦
    2020-12-06 19:06

    What you are trying to achieve is called mapping rather filtering. Here's a solution

    var array = [{id:1, value:"100", name:"dog"},
    {id:2, value:"200", name:"cat"},
    {id:3, value:"300", name:"fish"},
    {id:4, value:"400", name:"mouse"},
    {id:5, value:"500", name:"snake"}];
    var result = array.map(function(obj) {
        return {id: obj.id, value: obj.value};
    });
    
    console.log(result);

提交回复
热议问题