我的资料:
[
{
"configData":[
{
"repoSettings":[
{
"BaseURL":"45df16eb-55f9-48a9-9df8-428984524d7b",
"CurrentBranch":"Publish_1522323990",
"CurrentHeadSHA":"85c599a6ab3ecf8bfb469479da",
"RepositoryId":1528,
"RepositoryName":"45df16eb-55f94524d7b"
}
]
},
{
"pageSettings":[
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"index.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
}
]
},
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"about.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
},
{
"Sidebar":"default"
}
]
},
,
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"about.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
},
{
"Sidebar":"default"
},
{
"MenuBar":"default"
}
]
}
]
}
]
}
]
在此数据中,我要遍历pageSettings的所有对象以及内部的所有partials数组,并要遍历所有对象并找到匹配条件的所有索引。
假设我有“ Header”键和值“ default”对。现在,我想在此JSON中查找所有pageSettings对象和所有partials对象。如果找到键-值对,则为该pageSettings索引返回true。
有可能通过Lodash吗?还是可以提供摘要?
最佳答案
看一下这个。
您可以在数组上使用forEach()并尝试从局部函数中找出键和值。
let data = [
{
"configData":[
{
"repoSettings":[
{
"BaseURL":"45df16eb-55f9-48a9-9df8-428984524d7b",
"CurrentBranch":"Publish_1522323990",
"CurrentHeadSHA":"85c599a6ab3ecf8bfb469479da",
"RepositoryId":1528,
"RepositoryName":"45df16eb-55f94524d7b"
}
]
},
{
"pageSettings":[
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"index.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
}
]
},
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"about.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
},
{
"Sidebar":"default"
}
]
},
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"about.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Header":"default"
},
{
"Footer":"default"
},
{
"Sidebar":"default"
},
{
"MenuBar":"default"
}
]
},
{
"PageCss":[
],
"PageExternalCss":[
],
"PageExternalJs":[
],
"PageLayout":"default",
"PageMetaInfo":[
],
"PageMetacharset":"UTF-8",
"PageName":"about.html",
"PageSEODescription":"",
"PageSEOKeywords":"",
"PageSEOTitle":"",
"PageScripts":[
],
"PageStyles":[
],
"partials":[
{
"Sidebar":"default"
},
{
"MenuBar":"default"
}
]
}
]
}
]
}
]
function find(key, value) {
let matchingIndex = [];
data[0].configData[1].pageSettings.forEach((i, settingIndex) => {
i.partials.forEach(j => {
let matching = Object.keys(j).filter(k => k === key && j[k] === value);
if(matching && matching.length) {
matchingIndex.push(settingIndex);
};
})
})
console.log('Given values ('+key+','+value+') are found in setting index: '+ matchingIndex);
}
find('Header','default');
find('Sidebar','default')