gpt4 book ai didi

javascript - 如何从json中获取仅数字键值对

转载 作者:行者123 更新时间:2023-12-01 02:06:12 25 4
gpt4 key购买 nike

我有这样的 Json 数据

{
"info":[
{
"Name":"Noob Here",
"Major":"Language",
"Sex":"Male",
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"Name":"Noob Here",
"Major":"Calculus",
"Sex":"Female",
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}
]
}

我只需要获取值为数值数据的键值对。

预期输出

{
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
},
{
"English":"0.5",
"Japanese":"40",
"Calculus":"20",
"Geometry":"05"
}

是否有任何插件,或者我应该使用正则表达式为其编写长的 jquery 代码?

最佳答案

使用 jquery isNumeric 来查找是否为数字..然后使用对象和数组..您需要两个循环来获取对象...

给你..

var finalObj={};  //this is final output object
var info = []; //array to push to finalObj
$.each(data.info,function(i,v){
var object={};
$.each(v,function(i1,val1){
if($.isNumeric(val1)){

object[i1]=val1
}
});
info.push(object);
});
finalObj['info']=info;
console.log(finalObj);

fiddle here

关于javascript - 如何从json中获取仅数字键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15540904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com