如何搜索和returnjson对象?
How to search and return json object?
我需要搜索 return 一个 json 对象 children,遵循 json children 值。
这是我写的工作代码(带回正确的对象):
const sites = [
{
name: "Lightinthebox WW",
site_url: "https://lightinthebox.com/",
domain: "lightinthebox.com",
},
{
name: "Aliexpress WW",
site_url: "https://aliexpress.com/",
domain: "aliexpress.com",
},
{
name: "Dx WW",
site_url: "http://dx.com/",
domain: "dx.com",
}
];
var site = "aliexpress.com";
const foundSite = sites.find(s => site.includes(s.domain));
//return: {name: "Aliexpress WW", site_url: "https://aliexpress.com/", domain: "aliexpress.com"}
现在,我有一个不同的 JSON:
const sites = {"All":{"5631":{"id":5631,"name":"Lightinthebox WW","site_url":"https:\/\/lightinthebox.com\/","domain":"lightinthebox.com"},"6115":{"id":6115,"name":"Aliexpress WW","site_url":"https:\/\/aliexpress.com\/","domain":"aliexpress.com"},"7077":{"id":7077,"name":"Dx WW","site_url":"http:\/\/dx.com\/","domain":"dx.com","13318":{"id":13318,"name":"GearBest WW","site_url":"https:\/\/gearbest.com\/","domain":"gearbest.com"}}};
我尝试做同样的事情,但我有“sites.find 不是函数”
你能看出原因吗?试图寻找一种方法来带回正确的对象(搜索域:"aliexpress.com" 然后带回所有对象 "{"id":6115,"name":"Aliexpress WW","site_url":"https://aliexpress.com/","域":"aliexpress.com"}")
谢谢
Array.prototype.find()
是一个数组方法,在您的第一个示例中可以在 sites
上使用,因为它是一个数组,但在您的第二次尝试中,您试图在对象上调用它。
在这种情况下,您可以使用 Object.values()
到 return 顶级对象的 All
属性 中的值对象数组,并使用 .find()
关于那个。
(在这种情况下这不是 JSON related, but knowing the methods available for the class you are dealing with; Array vs Object)
const sites = {
"All": {
"5631": {
"id": 5631, "name": "Lightinthebox WW", "site_url": "https:\/\/lightinthebox.com\/", "domain": "lightinthebox.com"
},
"6115": {
"id": 6115, "name": "Aliexpress WW", "site_url": "https:\/\/aliexpress.com\/", "domain": "aliexpress.com"
},
"7077": {
"id": 7077, "name": "Dx WW", "site_url": "http:\/\/dx.com\/", "domain": "dx.com"
},
"13318": {
"id": 13318, "name": "GearBest WW", "site_url": "https:\/\/gearbest.com\/", "domain": "gearbest.com"
}
}
};
const site = "aliexpress.com";
const foundSite = Object.values(sites.All).find(s => site.includes(s.domain));
console.log(foundSite);
我需要搜索 return 一个 json 对象 children,遵循 json children 值。
这是我写的工作代码(带回正确的对象):
const sites = [
{
name: "Lightinthebox WW",
site_url: "https://lightinthebox.com/",
domain: "lightinthebox.com",
},
{
name: "Aliexpress WW",
site_url: "https://aliexpress.com/",
domain: "aliexpress.com",
},
{
name: "Dx WW",
site_url: "http://dx.com/",
domain: "dx.com",
}
];
var site = "aliexpress.com";
const foundSite = sites.find(s => site.includes(s.domain));
//return: {name: "Aliexpress WW", site_url: "https://aliexpress.com/", domain: "aliexpress.com"}
现在,我有一个不同的 JSON:
const sites = {"All":{"5631":{"id":5631,"name":"Lightinthebox WW","site_url":"https:\/\/lightinthebox.com\/","domain":"lightinthebox.com"},"6115":{"id":6115,"name":"Aliexpress WW","site_url":"https:\/\/aliexpress.com\/","domain":"aliexpress.com"},"7077":{"id":7077,"name":"Dx WW","site_url":"http:\/\/dx.com\/","domain":"dx.com","13318":{"id":13318,"name":"GearBest WW","site_url":"https:\/\/gearbest.com\/","domain":"gearbest.com"}}};
我尝试做同样的事情,但我有“sites.find 不是函数”
你能看出原因吗?试图寻找一种方法来带回正确的对象(搜索域:"aliexpress.com" 然后带回所有对象 "{"id":6115,"name":"Aliexpress WW","site_url":"https://aliexpress.com/","域":"aliexpress.com"}")
谢谢
Array.prototype.find()
是一个数组方法,在您的第一个示例中可以在 sites
上使用,因为它是一个数组,但在您的第二次尝试中,您试图在对象上调用它。
在这种情况下,您可以使用 Object.values()
到 return 顶级对象的 All
属性 中的值对象数组,并使用 .find()
关于那个。
(在这种情况下这不是 JSON related, but knowing the methods available for the class you are dealing with; Array vs Object)
const sites = {
"All": {
"5631": {
"id": 5631, "name": "Lightinthebox WW", "site_url": "https:\/\/lightinthebox.com\/", "domain": "lightinthebox.com"
},
"6115": {
"id": 6115, "name": "Aliexpress WW", "site_url": "https:\/\/aliexpress.com\/", "domain": "aliexpress.com"
},
"7077": {
"id": 7077, "name": "Dx WW", "site_url": "http:\/\/dx.com\/", "domain": "dx.com"
},
"13318": {
"id": 13318, "name": "GearBest WW", "site_url": "https:\/\/gearbest.com\/", "domain": "gearbest.com"
}
}
};
const site = "aliexpress.com";
const foundSite = Object.values(sites.All).find(s => site.includes(s.domain));
console.log(foundSite);