For with React的结构和实现



我要做的是接收"行";具有不定数量的值。

rows(功能输入(的示例值:[2, 5, 43, 191]

因此,我正在尝试实现for,这样我就可以构建我的数组,并将其放入查询中。

以下是我目前所拥有的:

const SelectFilterCustomer = async (rows) => {
let countrows = rows.length
if (countrows == 0) {
loadCustumers(0, customerList)
} else {
for (let i = 0; i < noGuest; i++) {
rows[0].clacte + ","
}
let idcliente = rows[0].clacte
var params = {
database: "forecast",
table: "customers",
queryString: "select * from customers where clacte in ( + idcliente + ) ",
};
api(athenaOperation, { params: JSON.stringify(params) }).then(
(allTodos) => {
let _f = JSON.parse(allTodos);
console.log(_f)
setCustomerList(_f.Items)
}
);
}
}

for的结果必须返回如下内容:[2, 5, 43, 191],因此我的查询结构如下:select * from customers where clacte in (2, 5, 43, 191)

只需使用Array.join连接数组中的值,指定的分隔符字符串为逗号和空格。示例:rows.join(', ')

join((方法通过连接数组(或类似数组的对象(中的所有元素来创建并返回一个新字符串,这些元素由逗号或指定的分隔符字符串分隔。如果数组只有一个项,则返回该项时将不使用分隔符。

const rows = [2, 5, 43, 191]
const queryString = `select * from customers where clacte in (${rows.join(', ')})`
const params = {
database: "forecast",
table: "customers",
queryString,
};
console.log(params)

我建议使用这种方法

const SelectFilterCustomer = async (rows) => {
let countrows = rows.length
if (countrows == 0) {
loadCustumers(0, customerList)
} else {
const idcliente = rows.map(rowItem => rowItem.clacte).join();
const params = {
database: "forecast",
table: "customers",
queryString: `select * from customers where clacte in ( ${idcliente} )`,
};
api(athenaOperation, { params: JSON.stringify(params) }).then(
(allTodos) => {
let _f = JSON.parse(allTodos);
console.log(_f)
setCustomerList(_f.Items)
}
);
}
}

请避免使用";var";用于定义变量的关键字。使用let或const;

最新更新