删除对象数组内的对象键和值

时间:2020-01-13 11:51:39

标签: javascript reactjs ecmascript-6 redux

我的代码=

这是我的示例数据。

const songs = [
  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group": {
          "group_id": "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
          "dept_short_name": "PAO",
          "dept_long_name": "Public Affairs Office"
        },
        "email": "john@doe.com"
      }
    ]
  }
]

    const newTest= songs.map(( {...song} ) => (
        song.performers.map(({...group}) => group_id = group.group_id)
    ))

我得到的结果是

enter image description here

我应该从组对象中删除 dept_short_name dept_long_name ,而 group_id 将保留下来,其余的数据歌曲应保持不变。

这应该是结果:

[
  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group_id":"1e5f73fa-ffe8-4c70-a83b-84e7bf985b25"
        "email": "john@doe.com"
      }
    ]
  }
]

4 个答案:

答案 0 :(得分:4)

您需要在两个地图中重新创建一个对象。

$row = null;
while($row_next = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if ($row !== null)) {
        $id = $row["ID"];
        $val_current = $row["VALUE"];
        $val_next = $row_next["VALUE"];
        if ($val_current > $val_next){
            echo "$id is greater!";
        }else{
            echo "$id is less!";
        }
        echo "\n";
    }
    $row = $row_next;
}

答案 1 :(得分:0)

  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group": {
          "group_id": "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
          "dept_short_name": "PAO",
          "dept_long_name": "Public Affairs Office"
        },
        "email": "john@doe.com"
      }
    ]
  }
]

window.addEventListener('load', function() {
 delete songs[0].performers[0].group.dept_short_name;
 delete songs[0].performers[0].group.dept_long_name;
 console.log(JSON.stringify(songs))
});

这可能有帮助。

答案 2 :(得分:0)

const songs = [{
  title: "Just Once",
  asset_id: "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
  sort: 1,
  performers: [{
    last_name: "John",
    first_name: "Doe",
    group: {
      group_id: "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
      dept_short_name: "PAO",
      dept_long_name: "Public Affairs Office"
    },
    email: "john@doe.com"
  }]
}];
songs.map(({ performers }, ind) => {
 performers.map(({ group }, ind) => {
  performers[ind].group_id = group.group_id;
 });

 delete performers[ind].group;
});

console.log(songs);

答案 3 :(得分:-1)

使用删除关键字: 删除表演者[“键名”] 它将返回布尔值