如何映射由ID过滤的对象数组

时间:2019-06-17 10:44:28

标签: javascript arrays json object

我有这个JSON结果。我想要做的是删除对象中的某些元素,并仅显示其中一些元素。问题是要到达我认为必须映射JSON才能到达的元素,但是在这种情况下该怎么做。

这是我的JSON:

[
      {
        "33274": {
          "idSon": 33274,
          "idMedia": 42084,
          "qfDiffusion": null,
          "qfAccent": null,
          "qfAge": 169,
          "qfCartoon": null,
          "qfDoublage": null,
          "qfInterpretation1": 194,
          "qfInterpretation2": 194,
          "qfInterpretation3": 193,
          "qfImitation": null,
          "qfLangue": 145,
          "qfPersonnage": null,
          "qfTimbre": 237,
          "qfChante": null,
          "qfType": 245,
          "qfGenre": "Masculin",
          "triRandom": 0,
          "timestampCreation": "2019-06-13T10:55:34.000Z",
          "timestampModification": "2019-06-13T10:55:34.000Z",
          "description": "Techno Music"
        }
      },
      {
        "33275": {
          "idSon": 33275,
          "idMedia": 42086,
          "qfDiffusion": null,
          "qfAccent": null,
          "qfAge": 240,
          "qfCartoon": null,
          "qfDoublage": null,
          "qfInterpretation1": 196,
          "qfInterpretation2": 195,
          "qfInterpretation3": 247,
          "qfImitation": null,
          "qfLangue": 147,
          "qfPersonnage": null,
          "qfTimbre": 236,
          "qfChante": null,
          "qfType": 176,
          "qfGenre": "Masculin",
          "triRandom": 0,
          "timestampCreation": "2019-06-13T11:05:48.000Z",
          "timestampModification": "2019-06-13T11:05:48.000Z",
          "description": "Techno Music"
        }
      }
    ]

这是所需的输出

[
      {
        "33274": {
          "idSon": 33274,
          "idMedia": 42084,
          "description": "Techno Music"
        }
      },
      {
        "33275": {
          "idSon": 33275,
          "idMedia": 42086,
          "description": "Techno Music"
        }
      }
    ]

我开始使用.map映射数组,但是我得到的是不确定的

let sonMp3 = await app.models.cm_comediens.getMp3ById(id);


{sonMp3.map(idSon => {
  console.log('testtt',        sonMp3[idSon]
  )
  return (
   sonMp3[idSon]
  );
})}

2 个答案:

答案 0 :(得分:1)

const mappedArr = arr.map(id => {
  mappedObj = {}
  Object.keys(id).forEach(key => {
    mappedObj[key] = {
      "idSon": id[key].idSon,
      "idMedia": id[key].idMedia,
      "description": id[key].description,
    }
  });
  return mappedObj;
})

答案 1 :(得分:0)

    arr.forEach((obj) => {
        Object.keys(obj).map((o) => {
delete obj[o]["qfAccent"],
delete obj[o]["qfAge"],
delete obj[o]["qfDiffusion"],
delete obj[o]["qfCartoon"],
delete obj[o]["qfDoublage"],
delete obj[o]["qfInterpretation1"],
delete obj[o]["qfInterpretation2"],
delete obj[o]["qfInterpretation3"],
delete obj[o]["qfImitation"],
delete obj[o]["qfLangue"],
delete obj[o]["qfPersonnage"],
delete obj[o]["qfTimbre"],
delete obj[o]["qfChante"],
delete obj[o]["qfType"],
delete obj[o]["qfGenre"],
delete obj[o]["triRandom"],
delete obj[o]["timestampCreation"],
delete obj[o]["timestampModification"],
        })
    });