如何从对象列表中检索值列表?
How to retrieve a list of values from a list of objects?
所以我有一个由 Sequelize 查询形成的对象列表:
[
Likes {
dataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_previousDataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
Likes {
dataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_previousDataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}
]
我在查询时遇到各种异常,因为我得到了这样的列表:[{PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd'}、{PlaceId: 'e678yt6-c846-46b5-aa25-6534d67326au'}] 或空列表:[]。
我尝试了 Object.values 以及 valueOf、map 和其他内置方法 - 甚至是循环手册,但我真的不明白为什么它们不起作用。您可以推荐哪些实现方法?
解决方案:
return models.Likes.findAll({
where: {
UserId: req.user.id
},
attributes: ['PlaceId']
}).then(likes => {
var values;
try {
values = likes.map(entity => entity.get('PlaceId'))
}
catch (e){
console.log(e);
}
models.Place.findAll({
where: {
id: {
[Sequelize.Op.notIn]: values
}
},
limit: 24
})
由于其中一项建议,这是最终的工作解决方案。使用 (entity => entity.get) 进行映射就可以了。向所有评论和提供帮助的人致以最诚挚的问候。
如果有人需要,它会从一个 table 执行后续查询,然后使用“NOT IN”查询另一个 table。在 SQL 中,查询 2 看起来像这样:
"SELECT "id", "coordinates", "place_name", "description", "category", "createdAt", "updatedAt" FROM "Places" AS "Place" WHERE "Place"."id" NOT IN ('e61d2360-c846-46b5-aa25-6534d38276dd', '417f55a6-1d64-491f-9c00-8f3094f3f53a') LIMIT 24;"
我会说,对你来说应该足够了
const FirstTable = sequelize.model(`FirstTable`)
const list = await FirstTable.findAll({
attributes: [`PlaceId`]
})
.then(data => {
return data.map(entity => entity.get('PlaceId'))
})
console.log(list)
输出将是:
[
'e61d2360-c846-46b5-aa25-6534d38276dd',
'e678yt6-c846-46b5-aa25-6534d67326au'
]
所以我有一个由 Sequelize 查询形成的对象列表:
[
Likes {
dataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_previousDataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
Likes {
dataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_previousDataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}
]
我在查询时遇到各种异常,因为我得到了这样的列表:[{PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd'}、{PlaceId: 'e678yt6-c846-46b5-aa25-6534d67326au'}] 或空列表:[]。
我尝试了 Object.values 以及 valueOf、map 和其他内置方法 - 甚至是循环手册,但我真的不明白为什么它们不起作用。您可以推荐哪些实现方法?
解决方案:
return models.Likes.findAll({
where: {
UserId: req.user.id
},
attributes: ['PlaceId']
}).then(likes => {
var values;
try {
values = likes.map(entity => entity.get('PlaceId'))
}
catch (e){
console.log(e);
}
models.Place.findAll({
where: {
id: {
[Sequelize.Op.notIn]: values
}
},
limit: 24
})
由于其中一项建议,这是最终的工作解决方案。使用 (entity => entity.get) 进行映射就可以了。向所有评论和提供帮助的人致以最诚挚的问候。
如果有人需要,它会从一个 table 执行后续查询,然后使用“NOT IN”查询另一个 table。在 SQL 中,查询 2 看起来像这样:
"SELECT "id", "coordinates", "place_name", "description", "category", "createdAt", "updatedAt" FROM "Places" AS "Place" WHERE "Place"."id" NOT IN ('e61d2360-c846-46b5-aa25-6534d38276dd', '417f55a6-1d64-491f-9c00-8f3094f3f53a') LIMIT 24;"
我会说,对你来说应该足够了
const FirstTable = sequelize.model(`FirstTable`)
const list = await FirstTable.findAll({
attributes: [`PlaceId`]
})
.then(data => {
return data.map(entity => entity.get('PlaceId'))
})
console.log(list)
输出将是:
[
'e61d2360-c846-46b5-aa25-6534d38276dd',
'e678yt6-c846-46b5-aa25-6534d67326au'
]