微信开发者工具 var currentGoods=this.data.goodsList[this.properties.LeftIndex].filter( x=>x.id===goodsindex)帮我将上述代码使用push方法追加到currentGoodsList数组中
时间: 2024-02-13 18:07:40 浏览: 25
可以使用以下代码将上述代码追加到currentGoodsList数组中:
```javascript
let currentGoodsList = this.data.currentGoodsList;
currentGoodsList.push(this.data.goodsList[this.properties.LeftIndex].filter(x => x.id === goodsindex));
this.setData({
currentGoodsList: currentGoodsList
});
```
这样就可以将上述代码追加到currentGoodsList数组中了。
相关问题
// 点击购物车事件 buttonClick(e){ // 当前商品的id号 var goodsindex=e.currentTarget.dataset.index console.log(goodsindex); // 筛选出当前选择的那个商品 // console.log(this.data.goodsList[this.properties.LeftIndex]); // 商品信息数组 let currentGoodsList = this.data.currentGoodsList; // 将点击过滤过的商品追加到currentGoodsList中 currentGoodsList.push(this.data.goodsList[this.properties.LeftIndex].filter(x => x.id === goodsindex)); // 将currentGoodsList数组赋值给data中的currentGoodsList this.setData({ currentGoodsList: currentGoodsList } ); console.log(this.data.currentGoodsList.id); //组织一个商品的信息对象 const goods={ goods_id:this.data.currentGoodsList.cmtcount, // 商品id goods_name:this.data.currentGoodsList.title, // 商品名称 goods_price:this.data.currentGoodsList.price, // 商品的价格 goods_count:1, // 商品的数量 goods_img:this.data.currentGoodsList.img, // 商品的图片 goods_state:true // 商品的勾选状态 } console.log(goods); // 将组织好的商品信息存储到store中 this.addToCart(goods) },帮我找出上述代码的bug
在代码第19行中,使用了数组的 filter 方法来过滤出当前选择的商品,但是 filter 方法的返回值是一个数组,而不是单个对象。因此,在第23行中,将该数组 push 到 currentGoodsList 数组中会导致 currentGoodsList 中存储的是一个嵌套数组,而不是单个对象。因此,需要将 filter 方法返回的数组中的第一个元素作为当前选择的商品对象,而不是直接将数组 push 到 currentGoodsList 数组中。修改代码如下:
```
// 点击购物车事件
buttonClick(e){
// 当前商品的id号
var goodsindex=e.currentTarget.dataset.index
console.log(goodsindex);
// 商品信息数组
let currentGoodsList = this.data.currentGoodsList;
// 筛选出当前选择的那个商品,并将其作为单个对象存储到 currentGoodsList 中
currentGoodsList.push(this.data.goodsList[this.properties.LeftIndex].find(x => x.id === goodsindex));
// 将currentGoodsList数组赋值给data中的currentGoodsList
this.setData({
currentGoodsList: currentGoodsList
});
console.log(this.data.currentGoodsList.id);
//组织一个商品的信息对象
const goods={
goods_id:this.data.currentGoodsList.cmtcount, // 商品id
goods_name:this.data.currentGoodsList.title, // 商品名称
goods_price:this.data.currentGoodsList.price, // 商品的价格
goods_count:1, // 商品的数量
goods_img:this.data.currentGoodsList.img, // 商品的图片
goods_state:true // 商品的勾选状态
}
console.log(goods);
// 将组织好的商品信息存储到store中
this.addToCart(goods)
}
```
// 商品排序 sortPrice() { if (this.value == 0) { this.goodsList = this.goodsList.sort((a, b) => { // console.log("000");0 return a.id - b.id; }); } else if (this.value == 1) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(111); return b.price - a.price; }); } else if (this.value == 2) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(222); return a.price - b.price; }); } else if (this.value == 3) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(333); return b.volume - a.volume; }); } else if (this.value == 4) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(444); return a.volume - b.volume; }); } }, // 点击有货优先时的回调函数 isHaveGoods() { // console.log(5555); // 只保留有货的商品 this.goodsList = this.goodsList.sort((a, b) => { return b.isHave - a.isHave; }); }, // 直营优先 isDirectGoods() { // console.log(6666); // 只保留有货的商品 this.goodsList = this.goodsList.sort((a, b) => { return b.isDirect - a.isDirect; }); },怎么把价格排序和直营优先排序结合起来
要把价格排序和直营优先排序结合起来,可以按照以下步骤进行:
1. 在前端页面上增加排序按钮或下拉菜单,包括价格排序和直营优先排序的选项。
2. 当用户选择价格排序选项时,调用 `sortPrice()` 函数进行价格排序。当用户选择直营优先排序选项时,调用 `isDirectGoods()` 函数进行直营优先排序。
3. 在 `sortPrice()` 函数中,增加直营优先的筛选条件,使排序结果中直营商品排在前面。具体实现可以在排序前先筛选出直营商品,然后再对直营商品和非直营商品分别进行价格排序,最后把排序后的直营商品和非直营商品合并起来。
4. 在 `isDirectGoods()` 函数中,增加价格排序的筛选条件,使排序结果中直营商品和非直营商品均按价格排序。具体实现可以先筛选出直营商品和非直营商品,然后分别对直营商品和非直营商品进行价格排序,最后把排序后的直营商品和非直营商品合并起来。
例如,在 `sortPrice()` 函数中可以按以下方式实现价格排序和直营优先排序的结合:
```javascript
sortPrice() {
if (this.value == 0) {
// 按商品ID排序
this.goodsList = this.goodsList.sort((a, b) => {
return a.id - b.id;
});
} else if (this.value == 1) {
// 按价格从高到低排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => b.price - a.price);
notDirectGoods.sort((a, b) => b.price - a.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 2) {
// 按价格从低到高排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => a.price - b.price);
notDirectGoods.sort((a, b) => a.price - b.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 3) {
// 按销量从高到低排序
this.goodsList = this.goodsList.sort((a, b) => {
return b.volume - a.volume;
});
} else if (this.value == 4) {
// 按销量从低到高排序
this.goodsList = this.goodsList.sort((a, b) => {
return a.volume - b.volume;
});
}
},
```
在 `isDirectGoods()` 函数中可以按以下方式实现价格排序和直营优先排序的结合:
```javascript
isDirectGoods() {
if (this.value == 1) {
// 直营优先,按价格从高到低排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => b.price - a.price);
notDirectGoods.sort((a, b) => b.price - a.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 2) {
// 直营优先,按价格从低到高排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => a.price - b.price);
notDirectGoods.sort((a, b) => a.price - b.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else {
// 直营优先,按直营优先排序
this.goodsList = this.goodsList.sort((a, b) => {
return b.isDirect - a.isDirect;
});
}
}
```
这样,就可以实现在点击其他筛选条件的时候还能进行价格由高到低或由低到高的排序,同时也能够实现直营优先的排序。