[英]Get Object Properties from Array<string> values
Thanks in advance, 提前致谢,
I have the following Array with different strings which match with different properties and positions into an object. 我有以下带有不同字符串的数组,这些字符串与对象的不同属性和位置匹配。
(5) ["payments[0]", "payments[0].bills[0]", "payments[1]", "payments[1].bills[0]", "payments[1].bills[1]"]
Object model is something like this and I need to access to every object into arrays(payments and bills) and get the key/values for each one : 对象模型就是这样,我需要将每个对象访问成数组(付款和账单),并获取每个对象的键/值:
Exception: In the case of payments I won't get the value of bills property 例外:在付款的情况下,我不会获得票据财产的价值
{"payments": [
{
"bills": [
{
"account": "1e329a4b-6c87-4001-bc84-5ef6214fa3ec",
"amount": "3243.00",
"autopay": false,
"bill": "b872f2ab-e446-4038-ae10-e0f94f8e4052",
"payment": "46413743-7849-44ed-8d9b-5b037bb6327e",
"customerReference": "Reference Customer",
"important": null
}
],
"totalAmount": "3243.00",
"guid": "46413743-7849-44ed-8d9b-5b037bb6327e"
},
{
"bills": [
{
"account": "1e329a4b-6c87-4001-bc84-5ef6214fa3ec",
"amount": "234.00",
"autopay": false,
"bill": "8cf5e681-eb70-43cd-824c-0a8535ad3456",
"payment": "3036bf6c-8919-4d30-a494-05493969988e",
"customerReference": "Reference customer",
"important": null
},
{
"account": "1e329a4b-6c87-4001-bc84-5ef6214fa3ec",
"amount": "234.00",
"autopay": false,
"bill": "8cf5e681-eb70-43cd-824c-0a8535ad3456",
"payment": "3036bf6c-8919-4d30-a494-05493969988e",
"customerReference": "Reference customer",
"important": null
}
],
"totalAmount": "234.00",
"guid": "3036bf6c-8919-4d30-a494-05493969988e"
}]}
After this, I need to create a request like this: 之后,我需要创建一个如下请求:
someURL/?payments[0].totalAmount=234&payments[0].bill[0].account=1e329a4b-6c87-4001-bc84-5ef6214fa3ec&payments[0].bill[0].account=234&ayments[0].bill[0].autopay=false&... someURL /?payments [0] .totalAmount = 234&payments [0] .bill [0] .account = 1e329a4b-6c87-4001-bc84-5ef6214fa3ec&payments [0] .bill [0] .account = 234&ayments [0] .bill [0 ] .autopay = false&...
Any Idea of how implement this ? 任何想法如何实施?
Thanks, 谢谢,
First you'll have to parse the string and get a path to your value. 首先,您必须解析字符串并获取指向您的值的路径。 Using a function like this:
使用这样的函数:
function parsePath(str) {
var byDot = str.split(".");
return byDot.reduce(function(path, part) {
var byBracket = part.split("[");
byBracket.forEach(function(part) {
path.push(part.replace("]", ""));
});
return path;
}, []);
}
Which, for example, returns ["payments", "0", "bills", "0", "account"]
if the string passed to it is "payments[0].bills[0].account"
. 例如,如果传递给它的字符串为
"payments[0].bills[0].account"
,则返回["payments", "0", "bills", "0", "account"]
。 You can add a check inside byBracket.forEach
to see if the subscript is a string or not and remove the quotes surrounding it, although that doesn't seem necessary as all the subscripts in your example appear to be numbers. 您可以在
byBracket.forEach
内部添加检查,以查看下标是否为字符串,并删除其周围的引号,尽管由于示例中的所有下标似乎都是数字,所以这似乎不是必需的。
Then resolve the value using a function like: 然后使用以下函数解析值:
function resolve(obj, path) {
return path.reduce(function(value, part) {
if(value && value.hasOwnProperty(part)) {
return value[part];
}
}, obj);
}
Which traverses the object level by level using the parts from the path
array. 它使用
path
数组中的零件逐级遍历对象。
Usage: 用法:
var value = resolve(myObject, parsePath(myString));
Example: 例:
function parsePath(str) { var byDot = str.split("."); return byDot.reduce(function(path, part) { var byBracket = part.split("["); byBracket.forEach(function(part) { path.push(part.replace("]", "")); }); return path; }, []); } function resolve(obj, path) { return path.reduce(function(value, part) { if(value && value.hasOwnProperty(part)) { return value[part]; } }, obj); } var myObject = {"payments":[{"bills":[{"account":"1e329a4b-6c87-4001-bc84-5ef6214fa3ec","amount":"3243.00","autopay":false,"bill":"b872f2ab-e446-4038-ae10-e0f94f8e4052","payment":"46413743-7849-44ed-8d9b-5b037bb6327e","customerReference":"Reference Customer","important":null}],"totalAmount":"3243.00","guid":"46413743-7849-44ed-8d9b-5b037bb6327e"},{"bills":[{"account":"1e329a4b-6c87-4001-bc84-5ef6214fa3ec","amount":"234.00","autopay":false,"bill":"8cf5e681-eb70-43cd-824c-0a8535ad3456","payment":"3036bf6c-8919-4d30-a494-05493969988e","customerReference":"Reference customer","important":null},{"account":"1e329a4b-6c87-4001-bc84-5ef6214fa3ec","amount":"234.00","autopay":false,"bill":"8cf5e681-eb70-43cd-824c-0a8535ad3456","payment":"3036bf6c-8919-4d30-a494-05493969988e","customerReference":"Reference customer","important":null}],"totalAmount":"234.00","guid":"3036bf6c-8919-4d30-a494-05493969988e"}]}; var strings = ["payments[0]", "payments[0].bills[0]", "payments[1]", "payments[1].bills[0]", "payments[1].bills[1]", "payments[0].totalAmount", "payments[0].bills[0].account", "payments[0].bills[0].account", "payments[0].bills[0].autopay"]; strings.forEach(function(str) { console.log(str + ": " + resolve(myObject, parsePath(str))); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.