从Array <string>值获取对象属性

时间:2018-04-18 10:56:36

标签: javascript javascript-objects

提前致谢,

我有以下数组,其中包含不同的字符串,这些字符串与不同的属性和位置匹配。

(5) ["payments[0]", "payments[0].bills[0]", "payments[1]", "payments[1].bills[0]", "payments[1].bills[1]"]

对象模型是这样的,我需要访问每个对象到数组(付款和账单)并获取每个对象的键/值:

例外:在付款的情况下,我不会获得账单资产的价值

{"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"
}]}

在此之后,我需要创建一个这样的请求:

someURL /付款[0] = .totalAmount 234安培;付款[0] .bill [0] = .account 1e329a4b-6c87-4001-bc84-5ef6214fa3ec&安培;付款[0] .bill [0] = .account 234安培; ayments [0] .bill [0] = .autopay假安培; ...

关于如何实现这一点的任何想法?

谢谢,

1 个答案:

答案 0 :(得分:0)

首先,您必须解析字符串并获取值的路径。使用这样的函数:

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;
    }, []);
}

例如,如果传递给它的字符串是["payments", "0", "bills", "0", "account"],则返回"payments[0].bills[0].account"。您可以在byBracket.forEach内添加一个检查以查看下标是否为字符串并删除它周围的引号,尽管这似乎没有必要,因为示例中的所有下标都显示为数字。

然后使用如下函数解析值:

function resolve(obj, path) {
    return path.reduce(function(value, part) {
        if(value && value.hasOwnProperty(part)) {
            return value[part];
        }
    }, obj);
}

使用path数组中的部分逐级遍历对象。

<强>用法:

var value = resolve(myObject, parsePath(myString));

示例:

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)));
});