【问题标题】:How to convert a serialized string into an array using jquery如何使用 jquery 将序列化的字符串转换为数组
【发布时间】:2018-12-21 01:47:37
【问题描述】:

我有这个字符串

foo = 'accordion-section[]=eat&accordion-section[]=pray&accordion-section[]=love'

如何在 jquery 中将其转换为array('eat', 'pray', 'love')

【问题讨论】:

  • 你可以用正则表达式来做到这一点
  • 为什么jQuery这个DOM操作库要负责字符串解析?这应该用javascript解决。

标签: jquery arrays string serialization


【解决方案1】:

它不是 jQuery,但您可以使用新的 URLSearchParams api 将值提取到新数组中。

注意: IE 不支持。

const foo = 'accordion-section[]=eat&accordion-section[]=pray&accordion-section[]=love&anotherValue=5';

const searchParams = new URLSearchParams(foo);

const result = searchParams.getAll('accordion-section[]');

console.log(result);

【讨论】:

  • 我接受这个答案,因为它是一种新的方式而且很短
【解决方案2】:

var foo = 'accordion-section[]=eat&accordion-section[]=pray&accordion-section[]=love';

//split the string by the & so you get key value pairs, and reduce them into a single object
var params = foo.split('&').reduce(function(results, keyValue){
  //split the key value pair by the =
  var [key, value] = keyValue.split('=');
  
  //if the key already exists, we need to convert the value to an array, and then push to the array
  if (results[key]) {
    if (typeof results[key] === 'string') {
      results[key] = [ results[key] ];
    }
    
    results[key].push(value);
  } else {
    //key did not exist, just set it as a string
    results[key] = value;
  }
  
  //return the results for the next iteration
  return results;
}, {});

//console.log the results
console.log(params);

【讨论】:

    【解决方案3】:

    function parse_query_string(query) {
      var vars = query.split("&");
      var query_string = {};
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        var key = decodeURIComponent(pair[0]);
        var value = decodeURIComponent(pair[1]);
        // If first entry with this name
        if (typeof query_string[key] === "undefined") {
          query_string[key] = decodeURIComponent(value);
          // If second entry with this name
        } else if (typeof query_string[key] === "string") {
          var arr = [query_string[key], decodeURIComponent(value)];
          query_string[key] = arr;
          // If third or later entry with this name
        } else {
          query_string[key].push(decodeURIComponent(value));
        }
      }
      return query_string;
    }
    
    foo = 'accordion-section[]=eat&accordion-section[]=pray&accordion-section[]=love';
    
    // as an object
    console.log(parse_query_string(foo))
    
    
    // as an array
    var fooArray = $.map(parse_query_string(foo), function(value, index) {
        return [value];
    });
    console.log(fooArray)
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2014-04-08
      相关资源
      最近更新 更多