如何在javscript中删除数组中的数组

时间:2019-02-01 11:00:59

标签: javascript arrays object filtering javascript-objects

let array = [
    {id: "455", some: [{id: "21"}]},
    {id: "12", some: [{id: "21"}]},
    {id: "12", some: [{id: "21"}]}
];

array.slice("some");

在这里,我试图删除数组中的一个数组,但是它不起作用。

在这里,我只想从所有对象中删除some数组。

3 个答案:

答案 0 :(得分:2)

您可以使用rest语法和一些Object Destructuring

let result = array.map(({ some, ...rest}) => rest);

演示:

let array = [
   {id: "455", some: [{id: "21"}]},
   {id: "12", some: [{id: "21"}]},
   {id: "12", some: [{id: "21"}]}
];
      
let result = array.map(({ some, ...rest}) => rest);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

一行:

let array = [{id:"455",some:[{id:"21",},],},{id:"12",some:[{id:"21",},],},{id:"12",some:[{id:"21",},],}];


array.forEach(it => delete it.some);

console.log(array);

答案 2 :(得分:1)

您可以使用mapdestructuring仅取出所需的属性和值。

let array = [{id:"455",some:[{id:"21",},],},{id:"12",some:[{id:"21",},],},{id:"12",some:[{id:"21",},],}];
      
let result = array.map(({id}) => ({id}));

console.log(result);