gpt4 book ai didi

javascript - 如何在 Jade 中做复杂的条件或正则表达式

转载 作者:行者123 更新时间:2023-11-30 18:06:23 25 4
gpt4 key购买 nike

我遇到了一个有趣的挑战,但不知道如何解决它。我正在从我的 mailchimp 中提取订阅者列表,并使用这些数据填充我网站上的成员(member)页面。我注册的字段之一是“网站或 LinkedIn 个人资料网址”。

我想做的是,检查他们是否提供了 linkedin 地址,如果是,请将他们的 url 放入 linkedin 成员(member)插件中,如果没有,请使用其他数据创建更简单的个人资料、名称网站、标题等.

我的问题是我不能说 data.websiteField.match(/linkedin/) 是否在 jade 中,所以我要么需要将数据传递给一些我遇到问题的客户端 javascript,要么做其他事情。

这是从 mailchimp 返回的数据示例

[ // this first bit is just the fields
[
"email address",
"first name",
"last name",
"role",
"organization",
"headline",
"website",
"areas of interest",
"please email me about:",
"member_rating",
"optin_time",
"optin_ip",
"confirm_time",
"confirm_ip",
"latitude",
"longitude",
"gmtoff",
"dstoff",
"timezone",
"cc",
"region",
"last_changed"
],
[ // and here's the first user
"woop@booomshakala.com", // haha, just hiding some data
"Woop",
"Shak",
"Tester",
"ShakaCo",
"Whooooooper",
"http://linkedin.com/in/costamichailidis", // hit me up sometime
"Creativity, Innovation, Ideas and Science",
"Job Placement, Internships, Recruitment & Retention, Technology & MOOCs, Measurement & Evaluation, Documentation & Dissemination",
2,
"2013-03-28 19:28:55",
"173.52.122.111",
"2013-03-28 19:29:12",
"173.52.122.111",
"40.7648000",
"-73.7775000",
"-5",
"-4",
"America/New_York", "US", "NY", "2013-03-28 19:29:12"
]
]

任何帮助都会很棒!

此外,我正在使用 express 。 Express 是否使本地人在客户端 javascript 中可用?

最佳答案

客户端正则表达式

Jade 允许您在行首使用 - 修饰符执行任意 javascript

- if (profile.websiteField.match(/linkedin/)
// render linkedin profile here
- else
// render simple profile here

服务器端正则表达式

我认为在服务器端格式化个人资料信息并将 renderLinkedIn bool 字段设置为 true 或 false 会更简单

function displaySignUpPage(req, res) {
var profile = formatMailChimpData()
// profile now looks like
// {
// "email address": "woop@booomshakala.com",
// "first name": "Noah",
// ...
// }

var linkedInRegex = /linkedin/;
profile.renderLinkedIn = linkedInRegex.test(profile.website) // set renderLinkedIn to true or false
// say your jade view is called signUpPage.jade
var pageData = {
title: 'Register',
profile: profile
}
res.render('signUpPage', pageData)
}


function formatMailChimpData() {
var mailChimpData = [
[
"email address",
"first name",
"last name",
"role",
"organization",
"headline",
"website"
// other fields truncated
],
[
"woop@booomshakala.com", // haha, just hiding some data
"Noah",
"Isaacson",
"Tester",
"ShakaCo",
"Whooooooper",
"http://www.linkedin.com/pub/noah-isaacson/59/6a2/553"
]
]


// mailChimp puts the keys as the first entry
var mailChimpFields = mailChimpData[0]
var mailChimpProfile = mailChimpData[1]
// make profile into key-value pairs
var keyValuePairs = mailChimpFields.map(function (field, index) {
var profileValue = mailChimpProfile[index]
var keyValuePair = [field, profileValue]
return keyValuePair
})
var profile = keyValuePairs.reduce(function(prev, pair) {
var key = pair[0]
var value = pair[1]
prev[key] = value
return prev
}, {})
return profile
}

关于javascript - 如何在 Jade 中做复杂的条件或正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com