0

I want to parse specific element from an existing viewModel and put it into a new array.

I have an applications viewModel which has tons of information of applicants. It is called in the following way:

 self.applications = ko.observableArray(@Html.Json(Model.ApplicationCompatibilities.Select(o => o.JsonForm)) || []);

enter image description here

So the "application" object has several elements in it as shown in the attached image above.

What I wanted to do was, I wanted to retrieve only the "applicationKey" element into a new array. So what I did was :

            self.previewApplication = ko.computed(function () {
                return ko.utils.arrayFilter(self.applications(), function (i) {
                    return i.application.applicationKey;
                });
            });

I intended to loop through the "self.applications()" viewModel and return only the "application.applicationKey". But what I actually get by doing that is:

enter image description here

I am just getting the same "applications" object inside the new viewModel. How can I get an array of only the "application.applicationKey" ?

Thanks! Please Help!

EDIT:

This works now! Now my viewModel.previewApplication() gives me a list of applicationKey's. Now what I want to do is, I want to add a link that loads a modal. And inside that modal I want to display the specific "application" that has a specific "applicationKey"

For example, the link to the modal will look like this:

<a href="#" data-toggle="modal" data-target="#previewApplicantModal" data-bind="attr: { 'data-applicationKey': application.applicationKey }">
     Preview Application
</a>

So If I inspect the element of the link, it has a new data-binding:

<a href="#" data-toggle="modal" data-target="#previewApplicantModal" data-bind="attr: { 'data-applicationKey': application.applicationKey }" data-applicationkey="abc976cfx">
    Preview Application
</a>

Finally I want to loop through the "application" object and return only "one" element of the object that has "abc976cfx" as its applicationKey.

Is there an easy way of doing this? I attached a screenshot for better explanation.

enter image description here

0

1 Answer 1

1

arrayFilter filters the array and the function returns true or false if the item matches your filter criteria or not. You want arrayMap instead.

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

4 Comments

Could you please tell me how to use it in this specific case?
Literally replace arrayFilter with arrayMap, everything else remains the same. The function for arrayMap returns whatever you want to map the item to, in this case a scalar value.
Thank you! It works now. Will you be able to answer my 2nd connecting question? Please take a look at my Edit if possible. Thank you so much!
Per site rules, additional questions should be submitted separately (but you can link back to the original); no one is going to look at a question that is marked as answered so you're killing your chances at getting an answer.

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.