这是我的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相匹配的variantAattributes的项目。
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
上使用该代码答案 0 :(得分:1)
您可以根据回调函数使用.filter()
来过滤arr
。如果当前对象的VariationAttributes
属性的.some()
名称或值属性不等于curState
对象中相应对象的属性,则回调函数将返回true,如下所示:
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);
答案 1 :(得分:1)
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
将具有过滤后的数组。
答案 2 :(得分:1)
看看这个。
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)