Javascript 如何通过ID获取json数组中的数据
我有个问题,请你帮帮我! 我有一个json数组:Javascript 如何通过ID获取json数组中的数据,javascript,jquery,html,ajax,json,Javascript,Jquery,Html,Ajax,Json,我有个问题,请你帮帮我! 我有一个json数组: "category" : [ { id: 1, product: [{id : product_1, type : ball}] }, { id : 2, product :[{id : product_2, type : pen}] } ] 我的问题是:如果我有一个链接,比如:then,我如何按id=1的类别获取产品信息? 感谢您的建议:)假设您的数据结构如下所示: var data = { "cat
"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
我的问题是:如果我有一个链接,比如:then,我如何按id=1的类别获取产品信息?
感谢您的建议:)假设您的数据结构如下所示:
var data = {
"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
}
function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data, 1);
// item.id, item.type
然后,通过如下方式搜索数组,可以在id==1的类别数组中找到该项:
var data = {
"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
}
function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data, 1);
// item.id, item.type
函数findId(数据,idToLookFor){
var categoryArray=data.category;
对于(变量i=0;i
如果将类别数组保存在categories
JS变量中,则可以基于简单的JavaScript(没有jQuery)筛选此数组,如下所示:
var result = categories.filter(function(element){
if (element.id == 2){
return true;
} else {
return false;
}
});
然后在result[0]中。product
中,您将拥有特定类别内的产品数组
看看有没有证据
PS.
filter()
方法在所有浏览器中都不可用,您可能需要应用一些代码,以便在需要但未找到时使其可用。这里有一些关于Mozilla开发者网络的文章,详细介绍了filter()
,以及在旧浏览器上运行所需的代码:。我认为OP希望从url中检索相应的数据
因此,第一步是将url映射到标识符
var urlParts = window.location.href.split(/\//)
//window.location.href = http://domain.com/category/1
// urlParts = ['http:', '', 'domain.com', 'category', '1']
var id = urlParts.pop();
var category = urlParts.pop();
然后使用JSON.parse从JSON字符串中检索数据:
var data = JSON.parse('{"category" : [{"id": 1, "product": [{"id" : "product_1", "type" : "ball"}] }, {"id" : 2, "product" :[{"id" : "product_2", "type" : "pen"}] } ]}');
//The data structure
// {"category" : [
// {"id": 1, "product" :
// [
// {"id" : "product_1", "type" : "ball"}
// ]},
// {"id" : 2, "product" :
// [
// {"id" : "product_2", "type" : "pen"}
// ]}
// ]}
最后,您可以使用id
和category
function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data.category, id);
// item.id, item.type
函数findId(数据,idToLookFor){
var categoryArray=data.category;
对于(变量i=0;i
这是一种一行完成的方法,无需使用:
function filterById(jsonObject, id) {return jsonObject.filter(function(jsonObject) {return (jsonObject['id'] == id);})[0];}
然后,如果您想获取id=2的对象,只需使用:
var selectedObject = filterById(yourJson['category'], 2);
然后selectedObject将获得此值,如您的示例所示:
{
id : 2,
product :[{id : product_2, type : pen}]
}
有一个缺点:旧浏览器(如IE8)不支持filter(),因此需要添加此polyfill代码以实现旧浏览器兼容性:
您可能已经找到了答案,但如果没有找到,您可以简单地使用逻辑运算符或(|
)
在您的例子中,可以使用jQuery的ajax()
方法。要根据id
s获取您的产品,只需使用以下简单逻辑:
var prodID1 = 1, prodID2 = 2;
if(this.id == prodID || this.id == prodID2) {
//write your JSON data, etc.
//push to array, etc.
}
它老了。但为什么不把马克作为答案呢?工作得很好。