JavaScript—在对象数组中运行forEach。是否要检查字符串中的子字符串并将其替换为新的子字符串
在JavaScript中,我在一个对象数组上运行forEch方法。希望运行my JSON并在满足条件时用新字符串替换字符串中的子字符串 问题是:而且没有定义 下面是包含JSON数据的代码。提前谢谢你的帮助。这里还有repl链接,因此您可以运行代码JavaScript—在对象数组中运行forEach。是否要检查字符串中的子字符串并将其替换为新的子字符串,javascript,arrays,json,string,object,Javascript,Arrays,Json,String,Object,在JavaScript中,我在一个对象数组上运行forEch方法。希望运行my JSON并在满足条件时用新字符串替换字符串中的子字符串 问题是:而且没有定义 下面是包含JSON数据的代码。提前谢谢你的帮助。这里还有repl链接,因此您可以运行代码 var dataJson = [{ "created_at": "2015-03-27 11:08:00+00:00", "city": "Pueblo", "first_name": "daniel", "last_n
var dataJson = [{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Pueblo",
"first_name": "daniel",
"last_name": "Falsetta",
"country": "US",
"artist_id": 6709,
"email": "8XC87gZdjX92@example.com",
"profile_image_url": "https://d153fwbf2sefnf.cloudfront.net/media/artist/d/a/daniel_falsetta_headshot.jpg"
},{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Dallas",
"first_name": "Suah",
"last_name": "Yu",
"country": "US",
"artist_id": 6708,
"email": "BATJP3mht4vd@example.com",
"profile_image_url": ""
},{
"created_at": "2015-03-27 11:08:00+00:00",
"city": "Ottawa ",
"first_name": "Makena",
"last_name": "Ablett",
"country": "CA",
"artist_id": 6710,
"email": "RTFheqUbixmy@example.com",
"profile_image_url": "/media/artist/image-4414.jpg"
},{
"created_at": "2015-03-27 10:53:40+00:00",
"city": "Pittsburgh",
"first_name": "Michelle",
"last_name": "Vecchio",
"country": "US",
"artist_id": 7388,
"email": "kpEFwMqV0Zip@example.com",
"profile_image_url": "http://d153fwbf2sefnf.cloudfront.net/media/artist/i/m/image_473.jpg"
}, {
"created_at": "2015-03-26 18:13:45+00:00",
"city": "",
"first_name": "Wendy",
"last_name": "Slavas",
"country": "",
"artist_id": 28,
"email": "aHrDLA3W5a6p@example.com",
"profile_image_url": ""
}
];
var profUrlFunc = function() {
var objTableArray = dataJson;
objTableArray.forEach(function(user) {
var httpsLinkCloudFront = "https://d153fwbf2sefnf.cloudfront.net"; //length: 37
var httpLinkCloudFront = "http://d153fwbf2sefnf.cloudfront.net"; //length: 36
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
if(user.profile_image_url) {
//if user profile image has HTTP /cloudfront prefix link remove it and add http://www.bucketfeet.com OR don't add anything
user.profile_image_url = placeHolderForLink.replace("http://d153fwbf2sefnf.cloudfront.net", "www.bucketfeet.com")
}
if (user.profile_image_url) {
//if user profile image has HTTPS /cloudfront prefix link remove it and add http://www.bucketfeet.com OR don't add anything
user.profile_image_url = placeHolderForLink.replace("https://d153fwbf2sefnf.cloudfront.net", "www.bucketfeet.com")
}
if (user.profile_image_url) {
//if no prefix(broken link:/media/artist/image-4414.jpg) add http://www.bucketfeet.com
var newModifiedLinkUrl = prefixLink.concat(placeHolderForLink)
user.profile_image_url = newModifiedLinkUrl;
console.log(user.profile_image_url);
}
else {
//if it is empty give it a default image
user.profile_image_url = "http://www.google.com/favicon.ico";
}
});
};
console.log(profUrlFunc());
弗雷奇
if(placeHolderForLink && placeHolderForLink.length) {
user.profile_image_url = (!placeHolderForLink.indexof(httpsLinkCloudFront)) ?
prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpsLinkCloudFront)+37)
:(!placeHolderForLink.indexof(httpLinkCloudFront)) ?
prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpLinkCloudFront)+36)
:
prefixLink+placeHolderForLink
}
或者,可读且可维护:
if (placeHolderForLink && placeHolderForLink.length) {
if (!placeHolderForLink.indexof(httpsLinkCloudFront)) {
var1 = prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpsLinkCloudFront)+37)
} elsif (!placeHolderForLink.indexof(httpLinkCloudFront)) {
var1 = prefixLink+placeHolderForLink.substr(placeHolderForLink.indexOf(httpLinkCloudFront)+36)
} else {
var1 = prefixLink+placeHolderForLink
}
user.profile_image_url = var1
}
试试这个
var profUrlFunc = function() {
var objTableArray = dataJson;
objTableArray.forEach(function(user) {
var httpsLinkCloudFront = "https://d153fwbf2sefnf.cloudfront.net";
var httpLinkCloudFront = "http://d153fwbf2sefnf.cloudfront.net";
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
var condition = new RegExp([httpsLinkCloudFront, httpLinkCloudFront].join('|'));
if (user.profile_image_url) {
if (!condition.test(user.profile_image_url)) {
user.profile_image_url = prefixLink + user.profile_image_url;
} else {
user.profile_image_url = user.profile_image_url.replace(condition, prefixLink);
}
} else {
user.profile_image_url = "http://www.google.com/favicon.ico";
}
});
return objTableArray;
};
您可以稍微简化您的功能:
首先,除了协议部分,
replace
函数available regex作为第一个参数之外,云前端有相同的url
/https?:\/\/d153fwbf2sefnf.cloudfront.net/
这个正则表达式表示s
在部分https
中是可选的,因此这个正则表达式将url与http和https匹配
其次,在添加前,应检查placeHolderForLink
是否尚未从prefixLink
开始
所以你可以得到这样的东西:
var profUrlFunc = function () {
var objTableArray = dataJson;
objTableArray.forEach(function (user) {
var LinkCloudFront = /https?:\/\/d153fwbf2sefnf.cloudfront.net/;
var prefixLink = "http://www.bucketfeet.com";
var placeHolderForLink = user.profile_image_url;
if (placeHolderForLink) {
placeHolderForLink = placeHolderForLink.replace(LinkCloudFront, prefixLink);
if (!placeHolderForLink.startsWith(prefixLink)) {
placeHolderForLink = prefixLink.concat(placeHolderForLink);
}
} else {
//if it is empty give it a default image
placeHolderForLink = "http://www.google.com/favicon.ico";
}
user.profile_image_url = placeHolderForLink;
});
};
var-dataJson=[{
“创建时间”:“2015-03-27 11:08:00+00:00”,
“城市”:“普韦布洛”,
“名字”:“丹尼尔”,
“姓氏”:“Falsetta”,
“国家”:“美国”,
“艺术家id”:6709,
“电子邮件”:8XC87gZdjX92@example.com",
“配置文件\图像\ url”:https://d153fwbf2sefnf.cloudfront.net/media/artist/d/a/daniel_falsetta_headshot.jpg"
}, {
“创建时间”:“2015-03-27 11:08:00+00:00”,
“城市”:“达拉斯”,
“名字”:“Suah”,
“姓”:“余”,
“国家”:“美国”,
“艺术家id”:6708,
“电子邮件”:BATJP3mht4vd@example.com",
“配置文件\图像\ url”:”
}, {
“创建时间”:“2015-03-27 11:08:00+00:00”,
“城市”:“渥太华”,
“名字”:“Makena”,
“姓氏”:“Ablett”,
“国家”:“CA”,
“艺术家id”:6710,
“电子邮件”:RTFheqUbixmy@example.com",
“profile_image_url”:“/media/artist/image-4414.jpg”
}, {
“创建时间”:“2015-03-27 10:53:40+00:00”,
“城市”:“匹兹堡”,
“名字”:“Michelle”,
“姓氏”:“Vecchio”,
“国家”:“美国”,
“艺术家id”:7388,
“电子邮件”:kpEFwMqV0Zip@example.com",
“配置文件\图像\ url”:http://d153fwbf2sefnf.cloudfront.net/media/artist/i/m/image_473.jpg"
}, {
“创建时间”:“2015-03-26 18:13:45+00:00”,
“城市”:“,
“名字”:“Wendy”,
“姓氏”:“斯拉瓦”,
“国家”:“,
“艺术家id”:28,
“电子邮件”:aHrDLA3W5a6p@example.com",
“配置文件\图像\ url”:”
}];
var profullfunc=函数(){
var objTableArray=dataJson;
forEach(函数(用户){
var LinkCloudFront=/https?:\/\/d153fwbf2sefnf.cloudfront.net/;
变量prefixLink=”http://www.bucketfeet.com";
var placeHolderForLink=user.profile\u image\u url;
if(占位符forlink){
Placeholder ForLink=Placeholder ForLink.replace(LinkCloudFront,prefixLink);
如果(!placeholder forlink.startsWith(prefixLink)){
占位符forlink=prefixLink.concat(占位符forlink);
}
}否则{
//如果为空,则为其提供默认图像
占位符FORLINK=”http://www.google.com/favicon.ico";
}
user.profile\u image\u url=占位符forlink;
});
};
profulfunc();
document.getElementById('r').innerHTML=JSON.stringify(dataJson.map)(函数(el){
返回el.profile\u图像\u url
}),空,2)代码>
你的问题是什么?@Alexander如果你在repl(上面的链接)上控制台代码,我会得到这个并且没有定义。我似乎无法解决这个问题。@Mihir Patel您得到的是未定义的
,因为您的函数profulfunc
不返回任何内容,在JS中默认为未定义的
,从函数返回您想要的内容。如您所见,没有未定义的
@MihirPatel当然,请稍等minute@MihirPatel,类似这样:,似乎我混淆了string和regex:-)对于家里的观众:永远不要这样写代码。还有,OP,请描述你的解决方案是如何工作的,提问者的问题是什么?vai你的推论。^我同意。我不明白这个解决方案。不起作用。js没有运算符elsif
:-)