0

I have a array with following structure in ko template

 styleBasedProductsDetails = {{
                "image_url": "media/images/home-page/1.jpg",
                "link_url": "push-up-bra-online/"
            },
            {
                "image_url": "media/images/home-page/5.jpg",
                "link_url": "push-up-bra-online/"
            },
            {
                "image_url": "media/images/home-page/2.jpg",
                "link_url": "bra-online/underwired-bra/"
            }}

How to split each element in this array like... styleBasedProductsDetails[0]['link_url'] without using foreach

0

That isn't an array, it's an object containing objects. An array would look like so (notice the [ and ]):

    styleBasedProductsDetails = [
        {
            "image_url": "media/images/home-page/1.jpg",
            "link_url": "push-up-bra-online/"
        },
        {
            "image_url": "media/images/home-page/5.jpg",
            "link_url": "push-up-bra-online/"
        },
        {
            "image_url": "media/images/home-page/2.jpg",
            "link_url": "bra-online/underwired-bra/"
        }
    ]

To loop through it in the template use the foreach binding:

<tbody data-bind="foreach: product">
    <tr>
        <td data-bind="text: link_url"></td>
        <td data-bind="text: image_url"></td>
    </tr>
</tbody>

That will output the links to text but you get the idea.


To loop through it in Javascript you don't need Knockout, use foreach

this.styleBasedProductsDetails.forEach(function(product) {
  console.log(product.link_url);
});
0

styleBasedProductsDetails()[0]['image_url']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.