简体   繁体   中英

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&...

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" . 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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