带引用的String到Object [英] String to Object with reference
问题描述
当字符串中有引用时,如何将字符串转换为对象?
字符串:
{field:OrderNumber,operator:eq,value:
e.data.OrderNumber}
我已经尝试过使用JSON.parse和eval,但我仍然在e.data.OrderNumber上收到错误
设置value
以清空字符串。使用 str
作为参数调用 JSON.parse()
。设置结果对象的 .value
。
var str =`{字段 : ORDERNUMBER, 运算符: 当量, 值: }`;
var obj = JSON.parse(str);
obj.value = e.data.OrderNumber;
另一种方法是利用 JSON.parse()
reviver
功能,此
使用功能设置为
。如果 e
.prototype.bind() key
等于value
,则在函数内创建一个变量 val
其中值最初设置为 undefined
。使用 .split()
在键
值中创建属性表示的数组
。 .slice()
结果数组中 0
之后的索引。使用 for..of
循环来获取 obj [arrayIndex]
的值到 obj [ arrayIndex] [propN]
其中 propN
是数组的第n个索引;将值赋给变量 val
。返回变量 val
跟循环,否则返回 value
参数。
var e = {data:{OrderNumber:1}}; var str ='{field:OrderNumber, operator:eq,value:e.data.OrderNumber}'; str = str.replace(/((\ w +。[\ w。] +)(?=(\s + |)\\ \\}))/ g,\$ 1 \); var rev = function(key,value){if(key ===value){let [props,val] = [value.split (。)。slice(1),void 0]; for(let prop of props){val = val === undefined?这[prop]:val [prop]; } return val; } return value;}; var obj = JSON.parse(str,rev.bind(e)); console.log(obj);
您还可以使用 String.prototype.replace()
使用 RegExp
/((\ w +。[\ w。] +)(?=(\s + |)\}) )/ g
匹配单词字符后跟。
后跟一个或多个单词字符和。
字符; String.prototype.match()
匹配单词字符并从匹配项中创建数组; Array.prototype.reduce()
迭代数组的 .length
的每个对象属性深度,返回数组深度的最后一个值 .length
对应于原始字符串中的对象属性。
const e = {data:{OrderNumber:1}};令str ='{field:OrderNumber,operator:eq,value :e.data.OrderNumber}'; str = str.replace(/((\\\ + +。[\ w。] +)(?=(\s + |)\}))/ g,function(匹配){let [,... props] = match.match(/[^.]+/ g); return props.reduce(function(obj,prop){return obj [prop]},e);}) ;让obj = JSON.parse(str); console.log(obj);
调用结果数组来自 .match()
, .shift()
,<$ c您可以在原始字符串上使用$ c> .split()通过解构赋值从 e
获取属性值;使用 .replace()
设置替换字符串,其中包含来自 e
的相应值;然后调用 JSON.parse()
,将结果字符串作为值来创建 javascript
对象。
const e = {data:{OrderNumber:1}}; let str ='{field :OrderNumber,operator:eq,value:e.data.OrderNumber}';让re = /((\\\\\\\ +)+(+ = |)\}))/ g;让obj = JSON.parse(str.replace(re,(()=>([,... props] = str.match(re).shift()。split (。),{[props [0]]:{[props [1]]:prop}} = e,prop))())); console.log(obj);
How can I convert a string to an object when there is a reference inside the string?
String:
{ "field": "OrderNumber", "operator": "eq", "value" : e.data.OrderNumber }
I already tried with JSON.parse and eval but I'm still getting an error on "e.data.OrderNumber"
Set "value"
to empty string. Call JSON.parse()
with str
as parameter. Set .value
of resulting object.
var str = `{"field":"OrderNumber","operator":"eq","value":""}`;
var obj = JSON.parse(str);
obj.value = e.data.OrderNumber;
An alternative approach would be to utilize JSON.parse()
reviver
function, with this
set to e
using Function.prototype.bind()
. If key
is equal to "value"
within the function create a variable val
where the value is initially set to undefined
. Use .split()
to create an array of the property representations within the key
"value"
. .slice()
the indexes following 0
from resulting array. Use for..of
loop to get the value of obj[arrayIndex]
through obj[arrayIndex][propN]
where propN
is the nth index of array; assign the value to the variable val
. Return the variable val
following loop, else return value
parameter.
var e = {
data: {
OrderNumber: 1
}
};
var str = '{"field":"OrderNumber","operator":"eq","value":e.data.OrderNumber}';
str = str.replace(/((\w+.[\w.]+)(?=(\s+|)\}))/g, "\"$1\"");
var rev = function(key, value) {
if (key === "value") {
let [props, val] = [value.split(".").slice(1), void 0];
for (let prop of props) {
val = val === undefined ? this[prop] : val[prop];
}
return val;
}
return value;
};
var obj = JSON.parse(str, rev.bind(e));
console.log(obj);
You can also retrieve and set the value without having the property name using String.prototype.replace()
with RegExp
/((\w+.[\w.]+)(?=(\s+|)\}))/g
to match word character followed by .
followed by one of more word characters and .
characters; String.prototype.match()
to match word characters and create an array from the matches; Array.prototype.reduce()
to iterate each depth of object properties for the .length
of the array, return the last value at depth of array .length
corresponding to object properties within original string.
const e = {
data: {
OrderNumber: 1
}
};
let str = '{ "field": "OrderNumber", "operator": "eq", "value" : e.data.OrderNumber }';
str = str.replace(/((\w+.[\w.]+)(?=(\s+|)\}))/g, function(match) {
let [,...props] = match.match(/[^.]+/g);
return props.reduce(function(obj, prop) {
return obj[prop]
}, e);
});
let obj = JSON.parse(str);
console.log(obj);
With resulting array from calls to .match()
, .shift()
, .split()
on original string you can get the property values from e
with destructuring assignment; use .replace()
to set replacement string with corresponding value from e
; then call JSON.parse()
with resulting string as value to create a javascript
object.
const e = {
data: {
OrderNumber: 1
}
};
let str = '{ "field": "OrderNumber", "operator": "eq", "value" : e.data.OrderNumber }';
let re = /((\w+.[\w.]+)(?=(\s+|)\}))/g;
let obj = JSON.parse(str.replace(re
, (() => ([,...props] = str.match(re).shift().split(".")
, {[props[0]]:{[props[1]]:prop}} = e, prop)
)()
));
console.log(obj);
这篇关于带引用的String到Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!