javascript - 在Javascript中按Id过滤json对象
问题描述
我有一个 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 的函数来管理它。任何帮助,将不胜感激。太感谢了
解决方案
尝试这个:
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;
推荐阅读
- c# - 无法访问 ASP.net core 3.0 中的用户机密
- android - 我只想在数据库表发生更改时向用户发送 Firebase 通知
- variables - 初始化自定义类型不适用于 var 但适用于 :=
- selenium - 隐式等待和 AjaxElementLocatorFactory 有什么区别?
- python - 如何打印字典中的最高值
- ruby-on-rails - Rails ActiveRecord 查询按天排序
- c# - 将“System.Windows.Forms.WebBrowser”转换为“OpenQA.Selenium.IWebDriver”
- blazor - 找不到路由值指定的后备端点:{ page: /_Host, area: }
- php - 注册后如何自动登录用户?
- c - 在 C 中解析十六进制字符串命令