我已经处于我想将其推送到数组中的大部分项目的状态
我想将所有状态推送到除某些项目之外
这是我的状态
state = {
username: '..',
date: '..',
time: '..',
description: '..',
images: '..',
buildingNumber: '..',
status: '..',
serviceDB: '..',
snapshotKey: '..', // i don't need this when pushed
count: -1, // i don't need this when pushed
};
这是我的代码
let order = [];
order.push(this.state);
console.log(order); it's log all state
// I want to push it to DB
database()
.ref(`Providers/ProvidersOrders/${uid}`)
.push(...order);
};
最佳答案
您可以使用 destructuring assignment和 rest parameters syntax如果您想在不使用任何库的情况下执行此操作:
const { snapshotKey, count, ...rest } = this.state;
...
order.push(rest);
另外,你也可以使用Lodash的_.omit
功能:
order.push(_.omit(this.state, ['snapshotKey', 'count']));
或者,如果您想选择要使用的属性,您可以再次使用解构和 shorthand property names创建对象:
const {
username,
date,
time,
description,
images,
buildingNumber,
status,
serviceDB,
} = this.state;
...
order.push({
username,
date,
time,
description,
images,
buildingNumber,
status,
serviceDB,
});
或者,对于 Lodash,使用 _.pick
,与 _.omit
相反:
order.push(_.pick(this.state, [
'username',
'date',
'time',
'description',
'buildingNumber',
'status',
'serviceDB',
]));
关于javascript - 如何将状态推送到包含特定项目的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59380600/