我正在尝试像这样过滤数据数组:
let data =
[
{
"approval": "TPED",
"manufacturer": "Chesterfield"
},
{
"approval": "BV",
"manufacturer": "Faber"
}
]
let approvalVariable = "TP"
let filteredData = data.filter(x => x.approval.includes(approvalVariable))
console.log(filteredData)
因此,如果rovalingVariable为“ TP”,我希望新数组为:
[
{
"approval": "TPED",
"manufacturer": "Chesterfield"
},
]
我可以在工作时使用它
let filteredData = data.filter(x => x.approval == approvalVariable)
但是当我尝试:
x.approval.includes(approvalVariable)
我收到x.approval.includes不是对象的错误
我曾经使用.includes()使其工作,但是现在出了点问题。
任何帮助将不胜感激。
componentWillMount() {
this.fetchData();
}
fetchData = async () => {
var fireBaseResponse = firebase.database().ref();
fireBaseResponse.once('value').then(snapshot => {
let data1 = [];
let approval = this.props.navigation.state.params.approval
let comments = this.props.navigation.state.params.comments
let designStandard = this.props.navigation.state.params.designStandard
let diameter = this.props.navigation.state.params.diameter
let h2Compatible = this.props.navigation.state.params.h2compatible
let inletThread = this.props.navigation.state.params.inletThread
let manufacturer = this.props.navigation.state.params.manufacturer
let specificationNumber = this.props.navigation.state.params.specificationNumber
let testPressure = this.props.navigation.state.params.testPressure
let waterCapacity = this.props.navigation.state.params.waterCapacity
let workingPressure = this.props.navigation.state.params.workingPressure
snapshot.forEach(item =>{
const temp = item.val();
data1.push(temp);
return false;
});
////////Filter Method/////////
if(approval == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.approval.includes(approval))
}
if(waterCapacity == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.waterCapacity == waterCapacity)
}
if(designStandard== '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.designStandard == designStandard)
}
if(diameter == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.diameter == diameter)
}
if(inletThread == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.inletThread == inletThread)
}
if(workingPressure == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.workingPressure == workingPressure)
}
if(comments == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.comments == comments)
}
if(manufacturer == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.manufacturer == manufacturer)
}
if(testPressure == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.testPressure == testPressure)
}
if(specificationNumber == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.specificationNumber == specificationNumber)
}
if(h2Compatible == '') {
console.log("good")
}
else {
data1 = data1.filter(x => x.h2Compatible == h2Compatible)
}
/////////////////////Filter Method//////////////////
this.setState({data: data1});
});
}
render(){
var {navigate} = this.props.navigation;
let {params} = this.props.navigation.state;
return(
<ViewContainer>
<ScrollView>
<FlatList
data = {this.state.data}
keyExtractor = {(x, i) => i}
renderItem ={({item}) =>
<Text style = {styles.itemText}>
Approval: {item.approval} | Manufacturer: {item.manufacturer} | Specification Number: {item.specificationNumber} |
H2 Compatible: {item.h2Compatible} | Diameter: {item.diameter} | Water Capacity: {item.waterCapacity} |
Inlet Thread: {item.inletThread}{"\n"}
</Text>
}
/>
</ScrollView>
<View style ={styles.footer}>
<TouchableOpacity style ={styles.footerButton} onPress = { () => navigate("ValveSearchScreen")}>
<Text style ={styles.footerButtonText}>SEARCH</Text>
</TouchableOpacity>
</View>
</ViewContainer>
)
}
}
答案 0 :(得分:2)
问题在于它正在数组对象中搜索一个名为include的属性。显然,它找不到它,因此警告我该财产不存在。为了解决这个问题,我将行更改为
let filteredData = data.filter(x => String(x.approval).includes(approvalVariable));
我希望这会在将来对其他人有所帮助,并且您不会像我一样花一个星期在没有帮助的情况下尝试解决这个问题。