英文:
How do I check if specific words contain in JS
问题
I have a problem checking if the word "product" is there.
添加一个名为 "newProduct" 的标头,其值为 "yes"。
Right now my problem is that if the collectionName
has a word of "e23product32", it doesn't add the header.
const productTags = [
"product"
];
const collectionName = 'e23product32';
const headers = {
...(productTags.some((tag) => tag.includes(collectionName)) && {
"newProduct": "yes",
}),
};
console.log(headers);
英文:
I have a problem checking if the the word "product" is there.
add a header called "newProduct" with a value of "yes".
Right now my problem is that if the collectionName
has a word of "e23product32", it doesn't add the header
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const productTags = [
"product"
];
const collectionName = 'e23product32';
const headers = {
...(productTags.some((tag) => tag.includes(collectionName)) && {
"newProduct": "yes",
}),
};
console.log(headers);
<!-- end snippet -->
答案1
得分: 1
你把支票拿反了... "product"
不包含(包括) "e23product32"
,但 "e23product32"
包含 "product"
。
const productTags = ["product"];
const collectionName = "e23product32";
const headers = {
...(productTags.some((tag) =>
collectionName.toLowerCase().includes(tag.toLowerCase())
) && {
newProduct: "yes",
}),
};
console.log(headers);
英文:
You've got your check back-to-front... "product"
does not contain (include) "e23product32"
but "e23product32"
does contain "product"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const productTags = ["product"];
const collectionName = "e23product32";
const headers = {
...(productTags.some((tag) =>
collectionName.toLowerCase().includes(tag.toLowerCase())
) && {
newProduct: "yes",
}),
};
console.log(headers);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论