从对象中删除 id 过滤器并使用 javascript 将其更改为简单格式

我有 resultMp3 给我这个:


"mp3": {

  "33274": {

    "idSon": 33274,

    "description": "Techno Music",

    "Typenom": "Billboard",

  },

  "33275": {

    "idSon": 33275,

    "description": "Techno Music",

    "Typenom": "Pub Cd/Dvd",


  }

},

我想简单地按 id 删除过滤器,所以结果将是这样的


"mp3": {

  {

    "idSon": 33274,

    "description": "Techno Music",

    "Typenom": "Billboard",

  },

  {

    "idSon": 33275,

    "description": "Techno Music",

    "Typenom": "Pub Cd/Dvd",

  }

},

到目前为止,这是我的功能。结果没问题,但我的问题是我想删除 Id 并仍然使用它进行过滤:


let resultMp3 = {};

let sqlPromise = await new Promise(function (resolve, reject) {


  var ds = app.datasources.mydb;

  var sql = `....`;


  ds.connector.execute(sql, [], function (err, cm_comediens_extraits_mp3s) {


  if (cm_comediens_extraits_mp3s.length === 0){

    resultMp3 = {}

  }else{

    for(var i=0; i<cm_comediens_extraits_mp3s.length;i++){        

      let extraitMp3 = cm_comediens_extraits_mp3s[i];

      let mp3 = {};

      mp3["idSon"] = extraitMp3.idSon;

      mp3["description"] = extraitMp3.description;

      mp3["Typenom"] = valueList[extraitMp3.type].fr;


      resultMp3[extraitMp3.idSon] = mp3;   


    }

  }

   resolve(resultMp3);   


  }); 

});

result.mp3=resultMp3;

任何帮助,将不胜感激。


侃侃尔雅
浏览 177回答 2
2回答

肥皂起泡泡

您可以使用 Object.valueslet data = { "mp3": {&nbsp; "33274": {&nbsp; &nbsp; "idSon": 33274,&nbsp; &nbsp; "description": "Techno Music",&nbsp; &nbsp; "Typenom": "Billboard",&nbsp; },&nbsp; "33275": {&nbsp; &nbsp; "idSon": 33275,&nbsp; &nbsp; "description": "Techno Music",&nbsp; &nbsp; "Typenom": "Pub Cd/Dvd",&nbsp; }}};let d = {"mp3" : Object.values(data.mp3)}console.log(d)

倚天杖

如果您的目标是制作对象数组,则可以使用 Object.values()let obj = {&nbsp; "mp3": {&nbsp; &nbsp; "33274": {&nbsp; &nbsp; &nbsp; "idSon": 33274,&nbsp; &nbsp; &nbsp; "description": "Techno Music",&nbsp; &nbsp; &nbsp; "Typenom": "Billboard",&nbsp; &nbsp; },&nbsp; &nbsp; "33275": {&nbsp; &nbsp; &nbsp; "idSon": 33275,&nbsp; &nbsp; &nbsp; "description": "Techno Music",&nbsp; &nbsp; &nbsp; "Typenom": "Pub Cd/Dvd",&nbsp; &nbsp; }&nbsp; }}obj.mp3 = Object.values(obj.mp3)console.log(obj)在您的情况下,您可以初始化resultMp3为数组(let resultMp3 = [];)并替换此行resultMp3[extraitMp3.idSon] = mp3;经过resultMp3.push(mp3);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript