Javascript 在对象中加载数据?
我不确定我是否做了正确的方法,我做的很像课堂风格。有没有办法使用Javascript 在对象中加载数据?,javascript,node.js,Javascript,Node.js,我不确定我是否做了正确的方法,我做的很像课堂风格。有没有办法使用loadProducts(data)在对象中加载数据,这样我就可以调用orderLines.getItemsType() 用法: const items = orderProducts.getItemsType(['abc', 'ddd']); 注意:它适用于node.js,不适用于浏览器 我想下一种方法可以帮助您采用it类方法并解决您的问题: class OrderProducts { constructor(data) {
loadProducts(data)
在对象中加载数据,这样我就可以调用orderLines.getItemsType()
用法:
const items = orderProducts.getItemsType(['abc', 'ddd']);
注意:它适用于node.js,不适用于浏览器 我想下一种方法可以帮助您采用it类方法并解决您的问题:
class OrderProducts {
constructor(data) {
this.data = data;
this.getItemsType = this.getItemsType.bind(this);
}
getItemsType(type) {
// return the data filtering by type
return this.data;
}
}
// usage
const orderProduct = new OrderProduct(data);
const items = orderProduct.getItemsType(['abc', 'ddd']);
首先,要将产品保存到属性中。我们将用一些虚拟数据加载属性 然后,我们可以使用filter筛选数据,并测试该项是否位于products数组中,如下所示:
const orderProducts={
//产品清单
产品:[],
//要装载的产品
loadProducts:函数(…数据){
此.products.push(…数据)
},
//获取传入的项目
getItemsType:函数(…类型){
返回此.products.filter(p=>type.includes(p))
}
}
orderProducts.loadProducts('abc','123','111','ddd')
const items=orderProducts.getItemsType('abc','ddd')
console.log(items)
您需要将loadProducts
数据保存到属性中,然后使用getItemsType
搜索属性…type
是什么意思?它被称为,或者将它们传递给另一个方法,就像我对…data
所做的那样,该方法也需要多个参数。
class OrderProducts {
constructor(data) {
this.data = data;
this.getItemsType = this.getItemsType.bind(this);
}
getItemsType(type) {
// return the data filtering by type
return this.data;
}
}
// usage
const orderProduct = new OrderProduct(data);
const items = orderProduct.getItemsType(['abc', 'ddd']);