0

I have an array like this.

var arr = [
    {
        Id:1533248,
        Name:"HP",
        ParentId:33113319,
        ParentName:"Brand Name"
    },
    {
        Id:1533764,
        Name:"Samsung",
        ParentId:33113319,
        ParentName:"Brand Name"
    }    
]

And a template.

var template = 'a_{ParentName}:{ParentId}_({Name}:{Id})*'

I am trying to generate a string from template. All i have is the array of objects. Also section inside () can be repeated and separated by pipe.

This is the desired output.

var result = 'a_brand-name:33113319_hp:1533248|samsung:1533764|sony:1533438';

There could be many objects in array with same ParentId. I am trying to use regular expression but failing. I am no good at regular expressions.
Please help.

1
  • A number of JavaScript libraries are good for this. One that comes to mind is "Mustache". It might have a slightly differing syntax from your template, but will also grant advanced capabilities like looping (over an array). Commented Dec 7, 2015 at 14:31

1 Answer 1

1

You can use JS RegExp's along with replace to swap out the variables in your template. Additionally you can sanitize your strings into the desired format (based on what I see, replace white-space with a hyphen and convert the string to lower-case) with replace. It sounds like you have more data than is displayed above, so you may need to keep multiple templates (i.e. one for each parent) stored in an object where the keys are the parentId. Using that structure, you can iterate through the array, updating your template for each parentId pretty quickly.

var arr = [
    {
        Id:1533248,
        Name:"HP",
        ParentId:33113319,
        ParentName:"Brand Name"
    },
    {
        Id:1533764,
        Name:"Samsung",
        ParentId:33113319,
        ParentName:"Brand Name"
    }    
];


var template = 'a_{ParentName}:{ParentId}_({Name}:{Id})*';
arr.forEach(function(obj, i) {
  Object.keys(obj).forEach(function(key) {
    var newKey;
        if (typeof(obj[key]) === 'string') {
      newKey = obj[key].replace(/ /g, '-').toLowerCase();
    } else {
      newKey = obj[key];
    }

    var regex = new RegExp("{" + key + "}");
    template = template.replace(regex, newKey);
  });
  if (template.indexOf('({Name}:{Id})') === -1 && i < arr.length - 1) {
    template = template.replace(/\*/, '|({Name}:{Id})*');
  } else if(i === arr.length - 1) {
    template = template.replace(/[\*\(\)]/g, '');
  }
});

https://jsfiddle.net/mw3dwau7/2/

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer but this is the result it is generating a_brand-name:33113319_hp:1533248|samsung
samsung is missing the id
sorry forgot to mention this {Name}:{Id}. This section can be repeated many times and separated by pipe character
Ok, so in your initial template string, is this a more accurate representation: 'a_{ParentName}:{ParentId}_{Name}:{Id}|{Name}:{Id}|*'?
i posted this as an example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.