1

I have JS object A like:

 { Name, NameFilter, NameType, ..., Desc, DescName, DescType, ... }

I want to build new object B by next rule:
If A contains field AbcFilter, then B.Abc = { value: A.Abc, filter: A.AbcFilter, type: A.AbcType} for each AbcFilter in A.
In other words, I want to iterate over the members of JS object and get only members, which name contains any string and get the field value by it's string name.

0

1 Answer 1

1

Just iterate normally and check whether the property name contains 'Filter':

var B = {}, i, prefix;

for(var prop in A) {
    if(A.hasOwnProperty(prop)) {
        i = prop.indexOf('Filter');
        if(i > -1) {
            prefix = prop.substr(0, i);
            B[prefix] = {
                value: A[prefix],
                filter: A[prop],
                type: A[prefix+'Type']
            };
        }
    }
}

Of course this works only under the assumption that 'Filter' is not contained in other property names.

Reference: String.prototype.indexOf, String.prototype.substr

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

Comments

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.