Javascript 使用JS/JQuery将大文本转换为缩写或短字符串

Javascript 使用JS/JQuery将大文本转换为缩写或短字符串,javascript,jquery,arrays,replace,Javascript,Jquery,Arrays,Replace,我需要将这个大字符串转换为abbrev(短)字符串文本 我写了这段代码,但只转换最后一个值(Cuba到CUB) 你能帮我吗 函数文本{ 如果($(window).width()您正在用最新的更改覆盖结果。Replace返回一个字符串,您需要替换每个部分。如果将其分配给输出,则保留原始的text值,而不替换它 function text_abbrev() { if ($(window).width() <= 480) { $('.nav-tabs li a').each

我需要将这个大字符串转换为abbrev(短)字符串文本

我写了这段代码,但只转换最后一个值(Cuba到CUB)

你能帮我吗

函数文本{
如果($(window).width()您正在用最新的更改覆盖结果。Replace返回一个字符串,您需要替换每个部分。如果将其分配给输出,则保留原始的
text
值,而不替换它

function text_abbrev() {
    if ($(window).width() <= 480) {
      $('.nav-tabs li a').each(function() {
        var text = $(this).text();
        text = text.replace('Tijuana', 'TIJ');
        text = text.replace('Monterrey', 'MTY');
        text = text.replace('Guadalajara', 'GDL');
        text = text.replace('Toluca', 'TLC');
        text = text.replace('Cancún', 'CUN');
        $(this).text(text.replace('Cuba', 'CUB'));
      });
    } else {}
  } 

您正在用最新的更改覆盖结果。Replace返回一个字符串,您需要替换每个部分。如果将其分配给输出,则保留原始的
文本
值,而不替换它

function text_abbrev() {
    if ($(window).width() <= 480) {
      $('.nav-tabs li a').each(function() {
        var text = $(this).text();
        text = text.replace('Tijuana', 'TIJ');
        text = text.replace('Monterrey', 'MTY');
        text = text.replace('Guadalajara', 'GDL');
        text = text.replace('Toluca', 'TLC');
        text = text.replace('Cancún', 'CUN');
        $(this).text(text.replace('Cuba', 'CUB'));
      });
    } else {}
  } 

虽然您已经接受了答案,但我想我会提供一种可能比您的原始方法更具响应性的替代方法,该方法将在窗口调整到480px以下的宽度时显示缩写文本,并在窗口调整到大于或等于480px的宽度时恢复原始文本:

// this function takes an object (the 'haystack') within which
// we search for the supplied value (the 'needle'):
function getKeyFromValue(haystack, needle) {

  // here we retrieve the keys of the supplied Object using
  // Object.keys, which returns an Array of those keys:
  return Object.keys(haystack)

    // here we filter the Array of keys, using an Arrow
    // function, to retain only the key(s) for which
    // the value stored in the Object with the current
    // key is exactly equal to the supplied needle to
    // search for:
    .filter(key => haystack[key] === needle)
    // we then take the first element of the retained
    // Array of keys, and return that to the calling
    // context:
    .shift();
}

// an object tying full names to abbreviated names:
var locationAbbreviations = {
  'Cancún': 'CUN',
  'Cuba': 'CUB',
  'Guadalajara': 'GDL',
  'Monterrey': 'MTY',
  'Tijuana': 'TIJ',
  'Toluca': 'TLC'
};

// binding the text-updates to the resize event(s) of
// the window:
$(window).resize(function() {

  // caching the elements:
  var elems = $('li a');

  // if the window width is less than 480px:
  if ($(this).width() < 480) {

    // we update the text of each of the found
    // elements, using the text() method's
    // anonymous function:
    elems.text(function(i, text) {
      // i:    the first argument, is the index of the
      //       current element in the jQuery collection
      //       element nodes.
      // text: the second argument, the current text
      // of the current element.

      // here we look up the abbreviation by using
      // the trimmed text of the current element 
      // (trimming removes leading and trailing
      // white-space from the String) as the key
      // with which to search in the
      // locationAbbreviations Object; if no value is
      // returned the response will be undefined
      // (falsey) so instead the original text will be
      // returned:
      return locationAbbreviations[text.trim()] || text;
    });
  } else {

    // otherwise, we instead use the text() method
    // and its anonymous function to return the
    // result of calling a function:
    elems.text(function(i, text) {

      // here we call the named function, supplying
      // both the Object ('haystack') and trimmed
      // string of text from the current element
      // ('needle'):
      return getKeyFromValue(locationAbbreviations, text.trim());
    })
  }
});
//此函数获取一个对象(“haystack”),其中
//我们搜索提供的值(“指针”):
函数getKeyFromValue(草堆、针){
//在这里,我们使用
//Object.keys,返回这些键的数组:
返回Object.keys(haystack)
//在这里,我们使用箭头过滤键数组
//函数,仅保留所需的密钥
//使用当前值存储在对象中的值
//键与所提供的针完全相同
//搜索:
.filter(键=>haystack[key]==针)
//然后,我们使用保留的元素的第一个元素
//数组,并将其返回给调用
//背景:
.shift();
}
//将全名与缩写名联系在一起的对象:
变量位置缩写={
"坎昆":"村",,
“Cuba”:“CUB”,
“瓜达拉哈拉”:“GDL”,
“蒙特雷”:“MTY”,
“蒂华纳”:“蒂吉”,
“托卢卡”:“TLC”
};
//将文本更新绑定到的调整大小事件
//窗口:
$(窗口)。调整大小(函数(){
//缓存元素:
变量元素=$('LIA');
//如果窗口宽度小于480px:
if($(this).width()<480){
//我们更新了每一个发现的文本
//元素,使用text()方法的
//匿名函数:
元素文本(函数(i,文本){
//第一个参数,是
//jQuery集合中的当前元素
//元素节点。
//text:第二个参数,当前文本
//当前元素的。
//在这里,我们使用
//当前元素的修剪文本
//(修剪将删除前导和尾随
//字符串中的空格)作为键
//用于在
//Location缩写对象;如果未指定任何值
//返回的响应将是未定义的
//(错误)因此,原文将改为
//返回:
返回位置缩写[text.trim()]| | text;
});
}否则{
//否则,我们将使用text()方法
//以及它的匿名函数来返回
//调用函数的结果:
元素文本(函数(i,文本){
//这里我们调用命名函数,提供
//对象(“干草堆”)和修剪对象
//来自当前元素的文本字符串
//(“针”):
返回getKeyFromValue(location缩写,text.trim());
})
}
});
函数getKeyFromValue(草堆、针){ return Object.keys(haystack).filter(key=>haystack[key]==针).pop(); } 变量位置缩写={ "坎昆":"村",, “Cuba”:“CUB”, “瓜达拉哈拉”:“GDL”, “蒙特雷”:“MTY”, “蒂华纳”:“蒂吉”, “托卢卡”:“TLC” }; $(窗口)。调整大小(函数(){ 变量元素=$('LIA'); if($(this).width()<480){ 元素文本(函数(i,文本){ 返回位置缩写[text.trim()]| | text; }); }否则{ 元素文本(函数(i,文本){ 返回getKeyFromValue(location缩写,text.trim()); }) } });


虽然您已经接受了答案,但我想我会提供一种可能比您原来的方法更具响应性的替代方法,当窗口调整到小于480px的宽度时,它将显示缩写文本,当窗口调整到大于或等于480px的宽度时,它将恢复原始文本:

// this function takes an object (the 'haystack') within which
// we search for the supplied value (the 'needle'):
function getKeyFromValue(haystack, needle) {

  // here we retrieve the keys of the supplied Object using
  // Object.keys, which returns an Array of those keys:
  return Object.keys(haystack)

    // here we filter the Array of keys, using an Arrow
    // function, to retain only the key(s) for which
    // the value stored in the Object with the current
    // key is exactly equal to the supplied needle to
    // search for:
    .filter(key => haystack[key] === needle)
    // we then take the first element of the retained
    // Array of keys, and return that to the calling
    // context:
    .shift();
}

// an object tying full names to abbreviated names:
var locationAbbreviations = {
  'Cancún': 'CUN',
  'Cuba': 'CUB',
  'Guadalajara': 'GDL',
  'Monterrey': 'MTY',
  'Tijuana': 'TIJ',
  'Toluca': 'TLC'
};

// binding the text-updates to the resize event(s) of
// the window:
$(window).resize(function() {

  // caching the elements:
  var elems = $('li a');

  // if the window width is less than 480px:
  if ($(this).width() < 480) {

    // we update the text of each of the found
    // elements, using the text() method's
    // anonymous function:
    elems.text(function(i, text) {
      // i:    the first argument, is the index of the
      //       current element in the jQuery collection
      //       element nodes.
      // text: the second argument, the current text
      // of the current element.

      // here we look up the abbreviation by using
      // the trimmed text of the current element 
      // (trimming removes leading and trailing
      // white-space from the String) as the key
      // with which to search in the
      // locationAbbreviations Object; if no value is
      // returned the response will be undefined
      // (falsey) so instead the original text will be
      // returned:
      return locationAbbreviations[text.trim()] || text;
    });
  } else {

    // otherwise, we instead use the text() method
    // and its anonymous function to return the
    // result of calling a function:
    elems.text(function(i, text) {

      // here we call the named function, supplying
      // both the Object ('haystack') and trimmed
      // string of text from the current element
      // ('needle'):
      return getKeyFromValue(locationAbbreviations, text.trim());
    })
  }
});
//此函数获取一个对象(“haystack”),其中
//我们搜索提供的值(“指针”):
函数getKeyFromValue(草堆、针){
//在这里,我们使用
//Object.keys,返回这些键的数组:
返回Object.keys(haystack)
//在这里,我们使用箭头过滤键数组
//函数,仅保留所需的密钥
//使用当前值存储在对象中的值
//键与所提供的针完全相同
//搜索:
.filter(键=>haystack[key]==针)
//然后,我们使用保留的元素的第一个元素
//数组,并将其返回给调用
//背景:
.shift();
}
//将全名与缩写名联系在一起的对象:
变量位置缩写={
"坎昆":"村",,
“Cuba”:“CUB”,
“瓜达拉哈拉”:“GDL”,
“蒙特雷”:“MTY”,
“蒂华纳”:“蒂吉”,
“托卢卡”:“TLC”
};
//将文本更新绑定到的调整大小事件
//窗口:
$(窗口)。调整大小(函数(){
//缓存元素:
变量元素=$('LIA');
//如果窗口宽度小于480px:
if($(this).width()<480){
//我们更新了每一个发现的文本
//元素,使用text()方法的
//匿名函数:
元素文本(函数(i,文本){
//i:第一个论点是