![](/img/trans.png)
[英]json/js/jquery how to filter multiple objects in nested json array
[英]How to filter out nested objects in JSON array?
这是我的 JSON 对象数组,其中包含嵌套对象:
var obj= [
{
"ASIN":"BCXXXXX1",
"VariationAttributes":[
{
"Name":"hand_orientation",
"Value":"Left Hand"
},
{
"Name":"shaft_material_type",
"Value":"KBS Max 90 Steel"
}
]
},
{
"ASIN":"BCXXXXX2",
"VariationAttributes":[
{
"Name":"hand_orientation",
"Value":"Right Hand"
},
{
"Name":"shaft_material_type",
"Value":"KBS Max 90 Steel"
}
]
}
]
var curState=[
{
"Name":"hand_orientation",
"Value":"Left Hand"
},
{
"Name":"shaft_material_type",
"Value":"KBS Max 90 Steel"
}
]
现在我想过滤掉具有与 curState 匹配的变体属性的项目。
var result = obj.map(m=>m.VariationAttributes).filter(search, curState);
function search(item){
return Object.keys(this).some((key) => item[key] === this[key]);
}
结果是空白。 你可以在https://playcode.io./518049玩代码
您可以使用.filter()
根据回调函数过滤您的arr
。 如果来自当前对象VariationAttributes
属性的.some()
名称或值属性不等于其在curState
对象中的相应对象的名称或值属性,则回调函数将返回 true,如下curState
:
const arr = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] }, { "ASIN": "BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Right Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] } ]; const curState = [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ]; const res = arr.filter( obj => obj.VariationAttributes.some(({Name, Value}, i) => curState[i] && (Name !== curState[i].Name || Value !== curState[i].Value)) ); console.log(res);
你也可以使用JSON.stringifiy()
在你的curState
这样你可以比较的字符串版本VariationAttributes
对VariationAttributes属性的字符串版本:
const arr = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] }, { "ASIN": "BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Right Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] } ]; const curState = [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ]; const strCurState = JSON.stringify(curState); const res = arr.filter( obj => strCurState !== JSON.stringify(obj.VariationAttributes) ); console.log(res);
var obj = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" }] },{"ASIN":"BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation","Value": "Right Hand"}, {"Name": "shaft_material_type", "Value": "KBS Max 90 Steel"}]}]; var curState = [{"Name": "hand_orientation", "Value": "Left Hand"},{"Name":"shaft_material_type", "Value": "KBS Max 90 Steel"}]; var finalResult = obj.filter(parentObj => { return JSON.stringify(parentObj.VariationAttributes) === JSON.stringify(curState); }); console.log(finalResult)
变量finalResult
将具有过滤后的数组。
看看这个。
var obj=[{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" }] },{"ASIN":"BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation","Value": "Right Hand"}, {"Name": "shaft_material_type", "Value": "KBS Max 90 Steel"}]}]; var curState=[{"Name": "hand_orientation", "Value": "Left Hand"},{"Name":"shaft_material_type", "Value": "KBS Max 90 Steel"}]; //var result = obj.map(m=>m.VariationAttributes).filter(search, curState); var result = obj.filter(item => { return item.VariationAttributes.every(attr => { return curState.find(state => attr.Name === state.Name && attr.Value === state.Value); }) }); console.log(result); function search(item){ return Object.keys(this).some((key) => item[key] === this[key]); } //console.log(obj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.