按 Javascript 中的 ID 过滤 json 个对象
filter json objects by Id in Javascript
我有一个 JSON return 对象数组,但我想做的是 return 按 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;
您可以使用 object
而不是 array
来执行此操作,这样您就可以输入 idSon
键作为限定符而不是 [=12= 中的索引]:
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;
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);
我知道有一个已经被接受的答案,但要以正确的方式执行此操作,您应该先获得总结果然后再进行处理。
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;
},{})
})
这样就不用每次都查询了
我有一个 JSON return 对象数组,但我想做的是 return 按 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;
您可以使用 object
而不是 array
来执行此操作,这样您就可以输入 idSon
键作为限定符而不是 [=12= 中的索引]:
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;
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);
我知道有一个已经被接受的答案,但要以正确的方式执行此操作,您应该先获得总结果然后再进行处理。
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;
},{})
})
这样就不用每次都查询了