Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将URL查询参数转换为格式化数组的最佳方法_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 将URL查询参数转换为格式化数组的最佳方法

Javascript 将URL查询参数转换为格式化数组的最佳方法,javascript,reactjs,redux,Javascript,Reactjs,Redux,我有一个带有查询参数的URL,看起来像这样 &rootDimension=可用性%253AOnlinepipsymbBrand%253ADell%253ABICpipsymbProduct%2520Rating%254A3%2520stars%2520及以上%2520 我正在做一个decodeURIComponent(query.rootRootdimension).split(“pipsymb”),它返回一个如下所示的数组 [“可用性:在线”、“品牌:戴尔:BIC”、“产品评级:四星级及以上”

我有一个带有查询参数的URL,看起来像这样
&rootDimension=可用性%253AOnlinepipsymbBrand%253ADell%253ABICpipsymbProduct%2520Rating%254A3%2520stars%2520及以上%2520

我正在做一个
decodeURIComponent(query.rootRootdimension).split(“pipsymb”)
,它返回一个如下所示的数组
[“可用性:在线”、“品牌:戴尔:BIC”、“产品评级:四星级及以上”]

我基本上需要检查阵列并删除不是“品牌”或“产品评级”的键。因此,在这种情况下,它应该返回一个数组
[“品牌:戴尔:BIC”,“产品评级:4星及以上”]

如果产品评级为“4星及以上”,则应将其替换为“最高评级”。如果不是,则应保持评级,例如
[“品牌:Dell:Bic”,“产品评级:3星及以上”]
。然后阵列应如下所示
[“品牌:戴尔:BIC”,“产品评级:最高评级”]

我要寻找的结果是
[“戴尔”、“Bic”、“顶级”]

我尝试了下面的功能和其他一些东西,但没有得到我想要的。谢谢你的帮助/建议

const getRefinements = (query) => {
  decodeURIComponent(query.rootDimension).split("pipsymb").reduce((obj, str) => {
            let strParts = str.split(/::|:/);
            if (strParts[0] && strParts[1]) {
                obj[strParts[0].replace(/\s+/g, "")] = strParts[1];
                return Object.values(pick(obj, ["Brand", "ProductRating"]))
            }
   })
}
请尝试以下操作:

let query=decodeURIComponent(
“&rootDimension=可用性%253AONLINEPISYMBRAND%253ADell%253ABICIPSYMBP产品%2520评级%254A3%2520星级%2520及以上%2520”
);
查询=查询
.替换(/%3A/g,“:”)
.替换(/%20/g,“”)
.替换(/%4A/g,“J”);
const productDetails=query.split(“pipsymb”);
让brandPart=productDetails
.find(item=>item.match(“品牌”))
.替换(“品牌:,”)
.拆分(“:”);
让productRating=productDetails
.find(item=>item.match(“产品评级”))
.分割(“J”)[1];
if(产品评级包括(“4”)){
productRating=“最高评级”;
}
const result=[…brandPart,productRating];
控制台日志(结果);
输出
如果您可以控制URL,则应将您想要的内容分成
&availability=Online&brand=Dell、BIC&productRating=4星及以上

这是因为您可以使用类似于已提供的
URLSearchParams
函数的功能更好地拆分它,而无需删除额外的字符串

let queryString = `&availability=Online&brand=Dell,BIC&productRating=4 stars and above`,
    urlParams   = new URLSearchParams(queryString),
    results     = {},
    arr         = [];

urlParams.forEach((v,k) => {
  if (k === "productRating") {
    if (v === "4 stars and above") {
      results.rating = "Top Rated";
    } else {
      results.rating = v;
    }
  } else if (k === "brand") {
    results.brands = v.split(","); // replace with ":" if delimiter cannot be changed
  }
});

// this should be what you get
// results = { 
//   rating : "Top Rated",
//   brands : ["Dell", "BIC"]
// }

// this will give you what you originally wanted, but it's not a very good data structure, use results instead if possible
arr = results.brands.concat(results.rating);
let queryString = `&availability=Online&brand=Dell,BIC&productRating=4 stars and above`,
    urlParams   = new URLSearchParams(queryString),
    results     = {},
    arr         = [];

urlParams.forEach((v,k) => {
  if (k === "productRating") {
    if (v === "4 stars and above") {
      results.rating = "Top Rated";
    } else {
      results.rating = v;
    }
  } else if (k === "brand") {
    results.brands = v.split(","); // replace with ":" if delimiter cannot be changed
  }
});

// this should be what you get
// results = { 
//   rating : "Top Rated",
//   brands : ["Dell", "BIC"]
// }

// this will give you what you originally wanted, but it's not a very good data structure, use results instead if possible
arr = results.brands.concat(results.rating);