【问题标题】:filter json objects by Id in Javascript在Javascript中按Id过滤json对象
【发布时间】:2019-10-30 20:44:29
【问题描述】:

我有一个 JSON,它返回一个对象数组的数组,但我想做的是返回一个按 ID 过滤的对象数组。 这是我到目前为止构建的代码:

    let objComedien3=[];
      let i=0;

      for (var prop in bc) {
        objComedien3[i] = await app.models.cm_comediens_extraits_mp3.find({ where: { idMedia: bc[prop]} } );
        //here the result is giving me an array of arrays of objects so I had to do this :
        objComedien3[i]= Object.assign({}, objComedien3[i]);
        i++;
      }
      return objComedien3;

我找到的最终结果是这样的:

[
  {
    "0": {
      "idSon": 33274,
      "idMedia": 42084,
      "qfDiffusion": null,
      "qfAccent": null,
      "qfAge": 169,
      "qfCartoon": null,
      "qfDoublage": null,
      "qfInterpretation1": 194,
      "qfInterpretation2": 194,
      "qfInterpretation3": 193,
      "qfImitation": null,
      "qfLangue": 145,
      "qfTimbre": 237,
      "qfType": 245,
      "qfGenre": "Masculin",
      "description": "Techno Music"
    }
  },
  {
    "0": {
      "idSon": 33275,
      "idMedia": 42086,
      "qfDiffusion": null,
      "qfAccent": null,
      "qfAge": 240,
      "qfCartoon": null,
      "qfDoublage": null,
      "qfInterpretation1": 196,
      "qfInterpretation2": 195,
      "qfInterpretation3": 247,
      "qfImitation": null,
      "qfLangue": 147,
      "qfTimbre": 236,
      "qfType": 176,
      "qfGenre": "Masculin",
      "description": "Techno Music"
    }
  }
]

一切都很好,除了我想用 idSon 替换 0 并且我不知道如何使用 Object.assign 或任何其他使用 javascript 的函数来管理它。 任何帮助,将不胜感激。非常感谢

【问题讨论】:

    标签: javascript node.js json object loopbackjs


    【解决方案1】:
     for (let t of test) {
    
        //replace your "0" with new "idSon"
    
        t["idSon"] = t["0"];
    
        //delete your "0" property object
    
        delete t["0"];
    
    }
    
    //filter based on Id in this example I Used 33275
    
    let filteredData: any = test.filter((o: any) => o["idSon"].idSon == 33275);
    

    【讨论】:

      【解决方案2】:

      我知道有一个已经被接受的答案,但要以正确的方式做到这一点,你应该得到总结果然后处理它们。

      const dataset = await app.models.cm_comediens_extraits_mp3.find(
          { where: { idMedia: {inq: bc} } }//which gets all the media
      ).then((data)=>{//it is an array already
          return data.reduce((p,c)=>{
              p[c.idSon] = c;//or c.__data if you want the data. Or Object.assign
              return p;
          },{})
      })
      

      这样你就不会每次都查询了。

      【讨论】:

        【解决方案3】:

        您可以通过使用object 而不是array 来做到这一点,这允许您输入idSon 作为限定符而不是array 中的索引:

        let objComedien3 = {};
        
        for (var prop in bc) {
          let result = await app.models.cm_comediens_extraits_mp3.find({ where: { idMedia: bc[prop]} } );
          let clonedResult = Object.assign({}, result[0]);
          objComedien3[clonedResult.idSon] = clonedResult;
        }
        
        return objComedien3;
        

        【讨论】:

          【解决方案4】:

          试试这个:

           let objComedien3=[];
                let i=0;
          
                for (var prop in bc) {
                  objComedien3[i] = await app.models.cm_comediens_extraits_mp3.find({ where: { idMedia: bc[prop]} } );
                  //here the result is giving me an array of arrays of objects so I had to do this :
                  let data= {}
                  data[objComedien3[i][0]['idSon']] = objComedien3[i][0]
                  objComedien3[i]= Object.assign({}, data);
                  i++;
                }
                return objComedien3;
          

          【讨论】:

          • 但理想情况下你应该做一个where: {idMedia: {inq: bc}} 然后得到整个结果集。并处理它们。这样您就不会使用多个查询。
          猜你喜欢
          • 2019-10-02
          • 1970-01-01
          • 2012-07-23
          • 2017-01-25
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多