在下面的代码中我有两个jsons,我必须根据json数据/值过滤json。
杰森大师:
var set1 =[{"LevelID":"1","levelname":"Level1",,"levelType":"TypeA"},
{"LevelID":"2","levelname":"Level2",,"levelType":"TypeB"},
{"LevelID":"3","levelname":"Level3",,"levelType":"TypeC"},
{"LevelID":"4","levelname":"Level4",,"levelType":"TypeD"}
];
Data Json:
var set2 =[{"LevelID":"1"},{"LevelID":"2"}];
var Commaseparated="TypeA","TypeD";
这里我想将Data json和commaseparated传递给Master json并过滤并获取应该与levelID和Commaseparated匹配的过滤数据。
预期结果:
var set3 =[{"LevelID":"1","levelname":"Level1","levelType":"TypeA"}];
答案 0 :(得分:0)
可能不是最好的方式,但这是你开始朝这个方向前进的一种方式。
var master = [{"LevelID":"1","levelname":"Level1","levelType":"TypeA"},
{"LevelID":"2","levelname":"Level2","levelType":"TypeB"},
{"LevelID":"3","levelname":"Level3","levelType":"TypeC"},
{"LevelID":"4","levelname":"Level4","levelType":"TypeD"}
];
var set2 = [{
"LevelID": "1"
}, {
"LevelID": "2"
}];
var Commaseparated = ["TypeA", "TypeD"];
const set3 = master.filter(item => {
return set2.find(item2 => item.LevelID === item2.LevelID)
}).filter(exclude => Commaseparated.indexOf(exclude.levelType) > -1);
/*
const set3 = set2.map(item => {
item.levelname = Commaseparated[item.LevelID - 1];
return item;
});
*/
console.log(set3);