简体   繁体   English

如何像查询字符串一样从字符串中获取特定值

[英]How to get specific value from string like in the query string

I have li with text id=452/name=Vova/Price=457$ i need in clien build var id,name,price and to each one get the right text that i can work with them This will look will look like this 我有li的文本id=452/name=Vova/Price=457$我需要在clien中构建var id,name,price,并向每个人获取正确的文本,我可以与他们一起使用。这看起来像这样

var id=452
var name=vova
var Price=457$
var liText = "id=452/name=Vova/Price=457$";

var vars = liText.split("/");
var obj = {};
vars.forEach(function(v) {
    var keyValue = v.split("=");
    obj[keyValue[0]] = keyValue[1];
});

Access them using the object notation obj["id"] or assign them to your variables if that is what you want. 使用对象表示法obj["id"]访问它们,或者根据需要将它们分配给变量。

var id = obj["id"];
var name = obj["name"];
var price = obj["Price"];

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM