gpt4 book ai didi

javascript - 在 Json 中搜索过滤器

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

我有一个像这样的 JSON 对象

 xyz = {
"101": {
"categoryName": "ectronics",
"categorslug": "electronics",
"catId": "101",
"name": [
{
"childCategoryName": "Cameras",
"childCategorySlug": "cameras",
"childCategoryId": "102",
"childCategoryParentId": "109"
},
{
"childCategoryName": "Accessories",
"childCategorySlug": "cameras-and-accessories",
"childCategoryId": "102",
"childCategoryParentId": "111"
},
{
"childCategoryName": "ras & Accessories",
"childCategorySlug": "cameras-and-accessories",
"childCategoryId": "102",
"childCategoryParentId": "112"
}
]
},
"109": {
"categoryName": "shion",
"categorslug": "fashion",
"catId": "109",
"name": [
{
"childCategoryName": "Fashion Accessories",
"childCategorySlug": "fashion-accessories",
"childCategoryId": "110",
"childCategoryParentId": "109"
}
]
},
"118": {
"categoryName": "Baby & Kids",
"categorslug": "baby-and-kids",
"catId": "118",
"name": [
{
"childCategoryName": "Baby Footwear",
"childCategorySlug": "baby-footwear",
"childCategoryId": "119",
"childCategoryParentId": "118"
}
]
}
}

现在我有一个基于搜索词的类别过滤器,我必须减少对象,我正在这样做,但问题是我无法从匹配的对象中减少它。

我尝试的解决方案是,

topIds = (event) => {
let rs = [];
let inn = '';
let cat = xyz;
for (let i in cat){
for( let j in cat[i].name ){
inn = cat[i].name[j].childCategoryName
if ( inn.toLowerCase().search(event.target.value.toLowerCase()) !== -1 ){
rs.push(cat[i].name[j].childCategoryParentId)
}
}
}
console.log('Filtered Ids', rs);
this.elm(cat, rs)
}

elm = (cat, rs) => {
let filtered = rs.map(function (k){
return cat[k]
})
console.log('Filtered va;ues', filtered);
return filtered;
}

它工作正常,但没有匹配它返回的完整对象,任何建议我在这里做错了什么。

所以,从技术上讲,如果用户说搜索“相机”

它应该像这样返回,

{
"101": {
"categoryName": "ectronics",
"categorslug": "electronics",
"catId": "101",
"name": [
{
"childCategoryName": "Cameras",
"childCategorySlug": "cameras",
"childCategoryId": "102",
"childCategoryParentId": "109"
}
]
}
}

提前谢谢你们。

最佳答案

据我了解..注意,您必须复制找到的对象,对于相应的子对象也是如此

const xyz = 
{ 101:
{ categoryName: 'ectronics'
, categorslug : 'electronics'
, catId : '101'
, name:
[ { childCategoryName : 'Cameras'
, childCategorySlug : 'cameras'
, childCategoryId : '102'
, childCategoryParentId: '109'
}
, { childCategoryName : 'Accessories'
, childCategorySlug : 'cameras-and-accessories'
, childCategoryId : '102'
, childCategoryParentId: '111'
}
, { childCategoryName : 'ras & Accessories'
, childCategorySlug : 'cameras-and-accessories'
, childCategoryId : '102'
, childCategoryParentId: '112'
} ] }
, 109:
{ categoryName: 'shion'
, categorslug : 'fashion'
, catId : '109'
, name:
[ { childCategoryName : 'Fashion Accessories'
, childCategorySlug : 'fashion-accessories'
, childCategoryId : '110'
, childCategoryParentId: '109'
} ] }
, 118:
{ categoryName: 'Baby & Kids'
, categorslug : 'baby-and-kids'
, catId : '118'
, name:
[ { childCategoryName : 'Baby Footwear'
, childCategorySlug : 'baby-footwear'
, childCategoryId : '119'
, childCategoryParentId: '118'
} ] } }

function getPath2childCategoryName(obj,val)
{
let child = null
, resp = {}
;
for(let key in obj)
{
child = obj[key].name.find(n=>n.childCategoryName===val)
if (child)
{
resp[key] = {...obj[key]}
resp[key].name = [ {...child} ]
break
}
}
return resp
}

let bob = getPath2childCategoryName(xyz,'Cameras')

console.log( JSON.stringify(bob,0,2) )
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 在 Json 中搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59888908/

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